我有以下資料框:
id phone email
10352897
10352897 10225967
10352897 [email protected]
10352897 10225967 [email protected]
10225967
10225967 [email protected]
[email protected]
23578910
23578910 38256789
23578910 [email protected]
23578910 38256789 [email protected]
38256789
38256789 [email protected]
[email protected]
我希望當它在三列中的至少一列中重合時,記錄連接并優先考慮填充欄位而不是空欄位,最后在這個例子中我希望得到以下輸出:
id phone email
10352897 10225967 [email protected]
23578910 38256789 [email protected]
你會怎么做呢?
uj5u.com熱心網友回復:
使用pandas.core.groupby.DataFrameGroupBy.aggregate如下
df_new = df.groupby('id').agg(lambda x: x.value_counts().index[0]).reset_index()
[Out]:
id phone email
0 10352897 10225967 [email protected]
1 23578910 38256789 [email protected]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/524024.html
標籤:Python熊猫
上一篇:Pandas:在由另一列值分組的列中獲取具有唯一值的唯一行
下一篇:如何從另一個資料幀回傳最大值
