[英]How to combine values in a dataframe pandas?
使用groupby_agg
。 如果您有其他列,請在需要時使用其他函數(max、min、first、last、...或 lambda)擴展 dict
out = df.groupby(['SellerName', 'SellerID'], as_index=False).agg({'Fruit': ', '.join})
print(out)
# Output
SellerName SellerID Fruit
0 Rob 200 Apple, Bannana
1 Scott 201 Apple, Kiwi, Pineapple
輸入數據框:
>>> df
SellerName SellerID Fruit
0 Rob 200 Apple
1 Scott 201 Apple
2 Rob 200 Bannana
3 Scott 201 Kiwi
4 Scott 201 Pineapple
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.