pandas 熊猫合并具有相同值和相同索引的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39646345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas merging rows with the same value and same index
提问by MScar
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.
我有一个 DataFrame 有一个索引叫做SubjectID
和一个 column Visit
。受试者具有多个访问和任一整数值或用于N / AValue1
和Value2
。我想折叠具有SubjectID
相同Visit
编号的行。
Here is my data frame:
这是我的数据框:
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
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()
解决这个问题,但我不知道如何使它同时考虑Visit
列中的索引和值。
回答by root
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:
您可以使用groupby.first
或groupby.last
获取组中每列的第一个/最后一个非空值。对于示例数据,两种方法的输出都相同:
df = df.groupby(['SubjectID', 'Visit']).first().reset_index()
The resulting output:
结果输出:
SubjectID Visit Value1 Value2
0 B1 1 1.57 1.75
1 B1 2 NaN 1.56