繁体   English   中英

计算 pandas 数据框中多列的变化率

[英]calculate rate of change for multiple columns in pandas data frame

附件图像是一个测试数据,其中包含多列缺失值。

我需要通过计算前 12 个月的变化率来填补缺失值

测试数据集

例如:在附加的数据集中,我在第 23 行和第 24 行中缺少 weight_a、weight_b、weight_c 列中的缺失值,以填充第 23 行中的缺失值,weight_a 列我需要做 =(B22-B10)/12 + B22 来填充我需要做的第24行weight_a列的缺失值=(B23-B11)/12 + B23

to fill the missing value in row 23, weight_b column I need to do =(C22-C10)/12 + C22
to fill the missing value in row 24, weight_b column I need to do =(C23-C11)/12 + C23 

依此类推...对 weight_c 列重复(并且真实数据集对多个列有很多缺失值)

我如何编写 python 代码来为 dataframe 中的所有缺失值实现这个?

计算值然后手动更新它们:

result_23=[1,2,3] # calculate real value instead of [1,2,3]
result_24=[1,2,3] # calculate real value instead of [1,2,3]

#Calculate them in this way, Based on what you want
#(df.iloc[22]["B"]-df.iloc[10]["B"]/12)+df.iloc[22]["B"]

df.loc[df.index==23,["weight_a","weight_b","weight_c"]]=result_23
df.loc[df.index==24,["weight_a","weight_b","weight_c"]]=result_24

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM