Python Pandas - 使用前一列的值向前填充整行

时间:2017-07-23 06:05:09

标签: python pandas dataframe data-science data-cleaning

熊猫开发新手。如何使用之前看到的列中包含的值转发填充DataFrame?

自包含的例子:

print(ohlc)
                     Open  High  Low  Close
2017-07-23 03:13:00   1.0   5.0  1.0    5.0
2017-07-23 03:14:00   NaN   NaN  NaN    NaN
2017-07-23 03:15:00   5.0   5.0  2.0    2.0
2017-07-23 03:16:00   NaN   NaN  NaN    NaN

这会产生以下DataFrame:

                     Open  High  Low  Close
2017-07-23 03:13:00   1.0   5.0  1.0    5.0
2017-07-23 03:14:00   5.0   5.0  5.0    5.0
2017-07-23 03:15:00   5.0   5.0  2.0    2.0
2017-07-23 03:16:00   2.0   2.0  2.0    2.0

我想从最后一个DataFrame转到这样的事情:

column2fill = 'Close'
ohlc[column2fill] = ohlc[column2fill].ffill()
print(ohlc)
                     Open  High  Low  Close
2017-07-23 03:13:00   1.0   5.0  1.0    5.0
2017-07-23 03:14:00   NaN   NaN  NaN    5.0
2017-07-23 03:15:00   5.0   5.0  2.0    2.0
2017-07-23 03:16:00   NaN   NaN  NaN    2.0

因此,“关闭”前向中先前看到的值会填充整行,直到看到新的填充行。这样填充“关闭”列非常简单:

{{1}}

但有没有办法在03:14:00和03:16:00行填充这些行的'Close'值?有没有办法在一步中使用一个前向填充而不是先填充“关闭”列?

1 个答案:

答案 0 :(得分:2)

您似乎需要assign ffill然后bfill每行axis=1,但必需的NaN行:

df = ohlc.assign(Close=ohlc['Close'].ffill()).bfill(axis=1)
print (df)
                     Open  High  Low  Close
2017-07-23 03:13:00   1.0   5.0  1.0    5.0
2017-07-23 03:14:00   5.0   5.0  5.0    5.0
2017-07-23 03:15:00   5.0   5.0  2.0    2.0
2017-07-23 03:16:00   2.0   2.0  2.0    2.0

与...相同:

ohlc['Close'] = ohlc['Close'].ffill()
df = ohlc.bfill(axis=1)
print (df)
                     Open  High  Low  Close
2017-07-23 03:13:00   1.0   5.0  1.0    5.0
2017-07-23 03:14:00   5.0   5.0  5.0    5.0
2017-07-23 03:15:00   5.0   5.0  2.0    2.0
2017-07-23 03:16:00   2.0   2.0  2.0    2.0