簡體   English   中英

使用Python中特定列的先前值填充NaN

[英]Fill in NaNs with previous values of a specific column in Python

我有一個Pandas數據幀如下:

df =

                      open       high        low      close
Timestamp                                                      
2014-01-07 13:18:00  874.67040  892.06753  874.67040  892.06753
2014-01-07 13:19:00        NaN        NaN        NaN        NaN
2014-01-07 13:20:00        NaN        NaN        NaN        NaN
2014-01-07 13:21:00  883.23085  883.23085  874.48165  874.48165
2014-01-07 13:22:00        NaN        NaN        NaN        NaN

對於每個NaN,他們應該取上一個時期的收盤價值。

編輯:我嘗試過使用df.fillna(method ='ffill'),但它會讓每個NaN直接在它上面取值。 我希望每個NaN在它之前只接受Close的值。

使用ffill產量:

                      open       high        low      close
Timestamp                                                      
2014-01-07 13:18:00  874.67040  892.06753  874.67040  892.06753
2014-01-07 13:19:00  874.67040  892.06753  874.67040  892.06753

但我正在尋找:

                      open       high        low      close
Timestamp                                                      
2014-01-07 13:18:00  874.67040  892.06753  874.67040  892.06753
2014-01-07 13:19:00  892.06753  892.06753  892.06753  892.06753

幾種方式:

In [3166]: df.apply(lambda x: x.fillna(df.close.shift())).ffill()
Out[3166]:
                          open       high        low      close
Timestamp
2014-01-07 13:18:00  874.67040  892.06753  874.67040  892.06753
2014-01-07 13:19:00  892.06753  892.06753  892.06753  892.06753
2014-01-07 13:20:00  892.06753  892.06753  892.06753  892.06753
2014-01-07 13:21:00  883.23085  883.23085  874.48165  874.48165
2014-01-07 13:22:00  874.48165  874.48165  874.48165  874.48165

In [3167]: df.fillna({c: df.close.shift() for c in df}).ffill()
Out[3167]:
                          open       high        low      close
Timestamp
2014-01-07 13:18:00  874.67040  892.06753  874.67040  892.06753
2014-01-07 13:19:00  892.06753  892.06753  892.06753  892.06753
2014-01-07 13:20:00  892.06753  892.06753  892.06753  892.06753
2014-01-07 13:21:00  883.23085  883.23085  874.48165  874.48165
2014-01-07 13:22:00  874.48165  874.48165  874.48165  874.48165

您可以填充關閉,然后回填軸1上的其余部分:

df.close.fillna(method='ffill', inplace=True)
df.fillna(method='backfill', axis=1, inpace=True)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM