pandas 合并具有相同值和相同索引的行 [英] Pandas merging rows with the same value and same index

查看:119
本文介绍了 pandas 合并具有相同值和相同索引的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataFrame,它的索引称为SubjectID和一列Visit.主题有多次访问,并且Value1Value2为整数值或不适用.我要折叠具有相同SubjectID和相同Visit编号的行.

I have a DataFrame with an index called SubjectID and a column Visit. Subjects have multiple Visits and either an integer value or an N/A for Value1 and Value2. I want to collapse the rows that have the same SubjectID and the same Visit number.

这是我的数据框:

SubjectID    Visit    Value1    Value2    
B1           1         1.57      N/A
B1           1         N/A       1.75
B1           2         N/A       1.56

我希望它看起来像这样:

I want to it to look like this:

Subject ID    Visit     Value1    Value2
B1            1          1.57      1.75
B1            2          N/A       1.56

我试图使用groupby()解决此问题,但是我不确定如何使其同时考虑到索引和Visit列中的值.

I was trying to use groupby() to solve this problem but I'm not sure how to make it take into account both the index and the values in the Visit column.

推荐答案

您可以使用

You can use groupby.first or groupby.last to get the first/last non-null value for each column within the group. For the example data, the output would be the same for either method:

df = df.groupby(['SubjectID', 'Visit']).first().reset_index()

结果输出:

  SubjectID  Visit  Value1  Value2
0        B1      1    1.57    1.75
1        B1      2     NaN    1.56

这篇关于 pandas 合并具有相同值和相同索引的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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