簡體   English   中英

如何組合數據框熊貓中的值?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM