简体   繁体   English

使用条件的熊猫缺失值(按其他列分组)

[英]Pandas missing values using conditions (groupby other columns)

I have a data frame like this我有一个这样的数据框

df df

Col A    Col B    Col C 

25       1         2          
NaN      3         1
27       2         3 
29       3         1

I want to fill Nan values in col A based on Col C and Col B.我想根据 Col C 和 Col B 填充 col A 中的 Nan 值。

My output df should be like this我的输出 df 应该是这样的

25       1         2          
29       3         1
27       2         3 
29       3         1

I have tried this code df.groupby(['Col B','Col C']).ffill()我试过这个代码df.groupby(['Col B','Col C']).ffill()

but didnt worked.Any suggestion will be helpful但没有用。任何建议都会有所帮助

Here you go:干得好:

df['Col A'] = df["Col A"].fillna(df.groupby(['Col B','Col C'])["Col A"].transform(lambda x: x.mean()))
print(df)

Prints:印刷:

   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

You can try你可以试试

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.

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