gpt4 book ai didi

python - 如何准确计算每天 DataFrame 中 NaN 的百分比变化?

转载 作者:太空宇宙 更新时间:2023-11-04 04:49:41 24 4
gpt4 key购买 nike

我想计算此 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() 方法也没有给我想要的结果。我想为没有值(value)或 NaN 的那一天计算一个有值(value)和 NaN 的每一天的值(value)。例如,在 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

提前致谢

最佳答案

这是一种方法,有点暴力。

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

关于python - 如何准确计算每天 DataFrame 中 NaN 的百分比变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48639281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com