我有一个看起来像这样的DataFrame

ID   Location1 Location2
AAA  Here      Null
AAA  Null      There
BBB  Here      Null
BBB  Null      There


我想做的就是将每个ID的所有内容都拉到一行上,以提供以下内容

ID   Location1 Location2
AAA  Here      There
BBB  Here      There


我在想也许我想使用groupbytransform

最佳答案

如果有可能获得每个组的第一个非Null值,请使用DataFrame.replaceGroupBy.first

df1 = df.replace('Null', np.nan).groupby('ID', as_index=False).first()
print (df1)
    ID Location1 Location2
0  AAA      Here     There
1  BBB      Here     There

关于python - 使用python中的通用ID将数据收集到一行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61633168/

10-12 22:29
查看更多