Pandas merging rows with the same value and same index

£可爱£侵袭症+ 提交于 2019-12-06 11:40:31

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!