pandas 合并具有相同值和相同索引的行 [英] Pandas merging rows with the same value and same index
问题描述
我有一个DataFrame,它的索引称为SubjectID
和一列Visit
.主题有多次访问,并且Value1
和Value2
为整数值或不适用.我要折叠具有相同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.
推荐答案
您可以使用 groupby.last
以获取组中每个列的第一个/最后一个非空值.对于示例数据,两种方法的输出将相同:
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屋!