[英]imputing missing dates for multiple columns in a dataframe based on businessdate index
[英]Imputing missing/cutover values across multiple columns in a dataframe
在以下情况下,我将如何 go 关于在 dataframe 中的多个列中估算缺失/切换值,如下所示:
df_input = pd.DataFrame({'ID': ['A','B','B','C'],
'Month1': [10, 15, 0, 0],
'Month2': [20, 32, 0 , 100],
'Month3': [0, 0, 45, 0],
'Month4': [50, 0, 51, 400]})
ID Month1 Month2 Month3 Month4
A 10 20 0 50
B 15 32 0 0
B 0 0 45 51
C 0 100 0 400
我预期的 output dataframe 将是:
df_output = pd.DataFrame({'ID': ['A','B','C'],
'Month1': [10, 15, 0],
'Month2': [20, 32, 100],
'Month3': [35, 45, 250],
'Month4': [50, 51, 400]})
ID Month1 Month2 Month3 Month4
A 10 20 35 50
B 15 32 45 51
C 0 100 250 400
国际大学联合会,
df_input = df_input.mask(df_input == 0)
df_out = df_input.groupby('ID').first()
df_out.interpolate(axis=1).fillna(0).reset_index()
Output:
ID Month1 Month2 Month3 Month4
0 A 10.0 20.0 35.0 50.0
1 B 15.0 32.0 45.0 51.0
2 C 0.0 100.0 250.0 400.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.