[英]Pandas missing values using conditions (groupby other columns)
我有一个这样的数据框
df
Col A Col B Col C
25 1 2
NaN 3 1
27 2 3
29 3 1
我想根据 Col C 和 Col B 填充 col A 中的 Nan 值。
我的输出 df 应该是这样的
25 1 2
29 3 1
27 2 3
29 3 1
我试过这个代码df.groupby(['Col B','Col C']).ffill()
但没有用。任何建议都会有所帮助
干得好:
df['Col A'] = df["Col A"].fillna(df.groupby(['Col B','Col C'])["Col A"].transform(lambda x: x.mean()))
print(df)
印刷:
Col A Col B Col C
0 25.0 1 2
1 29.0 3 1
2 27.0 2 3
3 29.0 3 1
你可以试试
df.fillna(df.groupby(['ColB','ColC']).transform('first'),inplace=True)
df
Out[386]:
ColA ColB ColC
0 25.0 1 2
1 29.0 3 1
2 27.0 2 3
3 29.0 3 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.