Pandas merging rows with the same value and same index

后端 未结 1 1379
一整个雨季
一整个雨季 2021-01-14 00:17

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

相关标签:
1条回答
  • 2021-01-14 01:07

    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()
    

    The resulting output:

      SubjectID  Visit  Value1  Value2
    0        B1      1    1.57    1.75
    1        B1      2     NaN    1.56
    
    0 讨论(0)
提交回复
热议问题