给定一个数据帧df
:
yellowCard secondYellow redCard
match_id player_id
1431183600x96x30 76921 X NaN NaN
76921 NaN X X
1431192600x162x32 71174 X NaN NaN
我想更新(相同索引的)重复行,结果是:
yellowCard secondYellow redCard
match_id player_id
1431183600x96x30 76921 X X X
1431192600x162x32 71174 X NaN NaN
pandas
是否提供了实现它的库方法?你知道吗
看起来您的df在} ,并填充
match_id
和player_id
上有多个索引,因此我将在match_id
上执行^{NaN
值两次,ffill和bfill:我使用以下代码构建上述内容,而不是使用
x
值:编辑groupby对象有^{} 和^{} 方法,因此简化为:
然后可以调用^{} :
如果你做了一个
NaN
的默认行为将忽略它们。对于示例中表单的数据帧(所有不一致都在NaN
和填充值之间),这将完成这项工作。你知道吗编辑
我假设
X
值是float的占位符。对于字符串,使用ffill
和bfill
的组合,比如EdChums answer(应该接受)。你知道吗相关问题 更多 >
编程相关推荐