首页 > 解决方案 > 使用 2 列在 pandas 中插入股票数据

问题描述

我想做股票报价的插值。我缺少一天的数据(如示例):

import numpy as np
import pandas as pd
dates = pd.date_range('20130101', periods=6)
df = pd.DataFrame(
    {'opening': [0.5, 1.3, np.NaN, 4, 5, 1],
     'closing': [0, 1, np.NaN, 2, 10, 2]}, index=dates)

            opening  closing
2013-01-01      0.5      0.0
2013-01-02      1.3      1.0
2013-01-03      NaN      NaN
2013-01-04      4.0      2.0
2013-01-05      5.0     10.0
2013-01-06      1.0      2.0

我需要有效插值 NaN 的方法,即closingof 2013-01-02is openingof2013-01-03openingof 2013-01-04is closingof 2013-01-03。期望的输出:

2013-01-01      0.5      0.0
2013-01-02      1.3      1.0
2013-01-03      1.0      4.0
2013-01-04      4.0      2.0
2013-01-05      5.0     10.0
2013-01-06      1.0      2.0

我试图使用应用,但它只有关于当前行的信息。我需要访问上一行和下一行。

标签: pandasinterpolation

解决方案


使用DataFrame.assign因为有必要通过向前或向后填充“并行”来替换 mssing 值:

df = df.assign(opening = df['opening'].fillna(df['closing'].ffill()),
               closing = df['closing'].fillna(df['opening'].bfill()))
print (df)
            opening  closing
2013-01-01      0.5      0.0
2013-01-02      1.3      1.0
2013-01-03      1.0      4.0
2013-01-04      4.0      2.0
2013-01-05      5.0     10.0
2013-01-06      1.0      2.0

推荐阅读