pandas 丢弃重复数据而忽略了NaN [英] Pandas drop duplicates ignoring NaN

查看:77
本文介绍了 pandas 丢弃重复数据而忽略了NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Pandas df中,我试图跨多列放置重复项。每行很多数据是 NaN

In a Pandas df, I am trying to drop duplicates across multiple columns. Lots of data per row is NaN.

这只是一个例子,数据杂乱无章,因此存在许多不同的组合。

This is only an example, the data is a mixed bag, so many different combinations exist.

df.drop_duplicates()

    IDnum       name            formNumber
1   NaN         AP GROUP        028-11964
2   1364615.0   AP GROUP        NaN
3   NaN         AP GROUP        NaN

有希望的输出:

    IDnum       name            formNumber
1   1364615.0   AP GROUP        028-11964

编辑:

如果 df。 drop_duplicates()看起来像这样,会改变解决方案吗? :

If the df.drop_duplicates() looks like this, would it change the solution? :

df.drop_duplicates()

    IDnum       name            formNumber
0   NaN         AP GROUP        028-11964
1   1364615.0   AP GROUP        028-11964
2   1364615.0   AP GROUP        NaN
3   NaN         AP GROUP        NaN


推荐答案

您可以先使用 groupby +

df.groupby('name',as_index=False).first()
Out[206]: 
      name      IDnum formNumber
0  APGROUP  1364615.0  028-11964

这篇关于 pandas 丢弃重复数据而忽略了NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆