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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:04:47  来源:igfitidea点击:

Pandas merging rows with the same value and same index

pandasindexingmergerows

提问by MScar

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

我有一个 DataFrame 有一个索引叫做SubjectID和一个 column Visit。受试者具有多个访问和任一整数值或用于N / AValue1Value2。我想折叠具有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 Visitcolumn.

我试图用来groupby()解决这个问题,但我不知道如何使它同时考虑Visit列中的索引和值。

回答by root

You can use groupby.firstor groupby.lastto 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.firstgroupby.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