2017-04-19 39 views
2

我想填充開放,高,低關閉值在數據幀中使用一行代碼。不知道爲什麼例1不起作用,其中例2呢。我在這裏錯過了什麼嗎?fillna()多個pandas列使用第三個

如果還有更好的方法可以做到這一點,我就會全神貫注。我使用前一時間段的收盤價來填寫開盤價,最高價和最低價的NaN值。我還設置卷至0

實施例1

import pandas as pd 

data = pd.read_pickle('../data/minute_bar_ESU9.pickle') 
data['ticker'] = 'ESU9' 
data['volume'].fillna(value=0, inplace=True) 
data['close'].fillna(method='ffill', inplace=True) 

data[['open','high','low']] = data[['open','high','low']].fillna(value=data.close) 

print(data.head(4)) 


         open high  low close volume ticker 
datetime               
2009-06-10 15:30:00 936.00 936.00 935.50 936.0 37.0 ESU9 
2009-06-10 15:31:00 935.75 935.75 935.50 935.5 26.0 ESU9 
2009-06-10 15:32:00  NaN  NaN  NaN 935.5  0.0 ESU9 
2009-06-10 15:33:00 935.75 936.00 935.75 936.0 13.0 ESU9 

實施例2:

import pandas as pd 

data = pd.read_pickle('../data/minute_bar_ESU9.pickle') 
data['ticker'] = 'ESU9' 
data['volume'].fillna(value=0, inplace=True) 
data['close'].fillna(method='ffill', inplace=True) 

data.open = data.open.fillna(value=data.close) 
data.high = data.open.fillna(value=data.close) 
data.low = data.open.fillna(value=data.close) 

print(data.head(4)) 


         open high  low close volume ticker 
datetime               
2009-06-10 15:30:00 936.00 936.00 936.00 936.0 37.0 ESU9 
2009-06-10 15:31:00 935.75 935.75 935.75 935.5 26.0 ESU9 
2009-06-10 15:32:00 935.50 935.50 935.50 935.5  0.0 ESU9 
2009-06-10 15:33:00 935.75 935.75 935.75 936.0 13.0 ESU9 

更新:看起來它使用實施例2完成快

Using: 
data = data.apply(lambda x: x.fillna(value=x.close),axis=1 
Total elapsed time: 42.797965 for shape: (131025, 6) 

Using: 
data.open = data.open.fillna(value=data.close) 
data.high = data.open.fillna(value=data.close) 
data.low = data.open.fillna(value=data.close) 
Total elapsed time: 0.055636 for shape: (131025, 6) 

Using: 
data = data.T.fillna(data.close).T 
Total elapsed time: 48.683746 for shape: (131025, 6) 

回答

2

請嘗試以下

data = data.apply(lambda x: x.fillna(value=x.close),axis=1) 
print(data.head(4)) 
2

例1您正試圖填補沿軸一個......或者水平失蹤。需要注意兩點:其一,您應該使用axis=1參數,其中兩個因爲尚未實現而不起作用。

df.fillna(df.close, axis=1) 

NotImplementedError: Currently only can fill with dict/Series column by column

變通
轉置數據,然後填寫

df.T.fillna(df.close).T 

         open high  low close volume ticker 
datetime               
2009-06-10 15:30:00  936  936 935.5 936  37 ESU9 
2009-06-10 15:31:00 935.75 935.75 935.5 935.5  26 ESU9 
2009-06-10 15:32:00 935.5 935.5 935.5 935.5  0 ESU9 
2009-06-10 15:33:00 935.75  936 935.75 936  13 ESU9 
相關問題