使用python中的公共ID将数据收集到一行

Tay*_*lrl 2 python pandas

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

ID   Location1 Location2
AAA  Here      Null
AAA  Null      There
BBB  Here      Null
BBB  Null      There
Run Code Online (Sandbox Code Playgroud)

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

ID   Location1 Location2
AAA  Here      There
BBB  Here      There
Run Code Online (Sandbox Code Playgroud)

我在想也许我可能想要使用groupbytransform?

jez*_*ael 5

如果有可能会首先非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
Run Code Online (Sandbox Code Playgroud)