简体   繁体   English

熊猫 - 卷起行来填补缺失的数据

[英]Pandas - rolling up rows to fill in missing data

I have an issue that I'm hoping has a relatively simple solution. 我有一个问题,我希望有一个相对简单的解决方案。 I have a dataframe with multiple records per unique contact (see "name" below). 我有一个数据帧,每个唯一的联系人有多个记录(见下面的“名称”)。 I'm trying to GroupBy the name and roll up values where they are missing. 我正在尝试GroupBy这个名称,并将值丢失的地方汇总。

   name   id            email
0   bob  5.0              NaN
1   bob  NaN     test@foo.com
2  bill  3.0              NaN
3  bill  NaN  something@a.com

The expected output would be: 预期的产出是:

   name   id            email
0   bob  5.0     test@foo.com  
1  bill  3.0     something@a.com 

I've tried Pivoting and Re-pivoting a few different ways to no avail. 我试过透视和重新转动几种不同的方法无济于事。 Is there any way I can do this? 有什么方法可以做到这一点吗?

You can try groupby.first() : 你可以尝试groupby.first()

df.groupby('name')[['id', 'email']].first()

#        id           email
#name       
#bill   3.0 something@a.com
#bob    5.0    test@foo.com

Also check the source code (according to which it takes the first non-null value) here if you are interested. 如果您有兴趣,还可以在此处检查源代码 (根据它获取第一个非空值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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