如何使用DataFrame中的NaN精确计算每天的百分比变化?

时间:2018-02-06 09:13:24

标签: python pandas dataframe

我想计算此DataFrame(frame _)的每日百分比更改:

import pandas as pd
import numpy as np
data_ = {
    'A':[1,np.NaN,2,1,1,2],
    'B':[1,2,3,1,np.NaN,1],
    'C':[1,2,np.NaN,1,1,2],
       }
dates_ = [
    '06/01/2018','05/01/2018','04/01/2018','03/01/2018','02/01/2018', '01/01/2018'
    ]

frame_ = pd.DataFrame(data_, index=dates_, columns=['A','B','C'])

问题是我使用此方法获取DataFrame:

returns_ = frame_.pct_change(periods=1, fill_method='pad')

dates,A,B,C
06/01/2018,,,
05/01/2018,,1.0,1.0
04/01/2018,1.0,0.5,
03/01/2018,-0.5,-0.6666666666666667,-0.5
02/01/2018,0.0,,0.0
01/01/2018,1.0,0.0,1.0

这不是我要找的。而dropna()方法也没有给我我寻求的结果。我想计算每天有价值的值和没有价值或NaN的那天的NaN。例如,在A栏上:作为百分比变化,我希望看到

dates,A
06/01/2018,1
05/01/2018,
04/01/2018,1.0
03/01/2018,-0.5
02/01/2018,0.0
01/01/2018,1.0

非常感谢提前

1 个答案:

答案 0 :(得分:1)

这是一种方式,有点蛮力。

import pandas as pd
import numpy as np
data_ = {
    'A':[1,np.NaN,2,1,1,2],
    'B':[1,2,3,1,np.NaN,1],
    'C':[1,2,np.NaN,1,1,2],
       }
dates_ = [
    '06/01/2018','05/01/2018','04/01/2018','03/01/2018','02/01/2018', '01/01/2018'
    ]

frame_ = pd.DataFrame(data_, index=dates_, columns=['A','B','C'])
frame_ = pd.concat([frame_, pd.DataFrame(columns=['dA', 'dB', 'dC'])])

for col in ['A', 'B', 'C']:
    frame_['d'+col] = frame_[col].pct_change()
    frame_.loc[pd.notnull(frame_[col]) & pd.isnull(frame_['d'+col]), 'd'+col] = frame_[col]

#               A    B    C   dA        dB   dC
# 06/01/2018  1.0  1.0  1.0  1.0  1.000000  1.0
# 05/01/2018  NaN  2.0  2.0  NaN  1.000000  1.0
# 04/01/2018  2.0  3.0  NaN  1.0  0.500000  NaN
# 03/01/2018  1.0  1.0  1.0 -0.5 -0.666667 -0.5
# 02/01/2018  1.0  NaN  1.0  0.0       NaN  0.0
# 01/01/2018  2.0  1.0  2.0  1.0  0.000000  1.0