亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

pandas Groupby 求和并連接

pandas Groupby 求和并連接

慕虎7371278 2023-03-30 16:21:16
我有一個數據框如下+-----------+----------+-----+| InvoiceNo | ItemCode | Qty |+-----------+----------+-----+|  Inv-001  |     c    |  1  |+-----------+----------+-----+|  Inv-001  |     b    |  2  |+-----------+----------+-----+|  Inv-001  |     a    |  1  |+-----------+----------+-----+|  Inv-002  |     a    |  3  |+-----------+----------+-----+|  Inv-002  |     b    |  1  |+-----------+----------+-----+|  Inv-002  |     c    |  1  |+-----------+----------+-----+|  Inv-002  |     d    |  4  |+-----------+----------+-----+|  Inv-002  |     a    |  1  |+-----------+----------+-----+|  Inv-003  |     e    |  1  |+-----------+----------+-----+|  Inv-003  |     b    |  2  |+-----------+----------+-----+我想計算每個單獨的InvoiceNo明智項目組合。即每個的總和ItemCode。排序并連接到一個字符串。注意:在Inv-002產品中a有 2 行。我想要/需要的輸出如下+-----------+--------------------+| InvoiceNo |   Desired result   |+-----------+--------------------+|  Inv-001  |    a-1, b-2, c-1   |+-----------+--------------------+|  Inv-002  | a-4, b-1, c-1, d-4 |+-----------+--------------------+|  Inv-003  |      b-2, e-1      |+-----------+--------------------+到目前為止我已經寫了下面的代碼#load datadf = pd.read_excel('data.xlsx')#groupby and sumg = df.groupby(['InvoiceNo','ItemCode']).sum()# Codes to convert the MultiIndex to a regualr dataframeg = g.unstack(fill_value=0)g.reset_index(drop=True,inplace=True)g = g.droplevel(level=0, axis=1).fillna(0)#calculationg.dot(g.columns+',').str[:-1]下面是我得到的結果。所有項目分開。+---+---------------------+| 0 |       a,b,b,c       |+---+---------------------+| 1 | a,a,a,a,b,c,d,d,d,d |+---+---------------------+| 2 |        b,b,e        |+---+---------------------+請指導我解決這個問題。
查看完整描述

2 回答

?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

groupby兩次。第一個獲得每個的總和['InvoiceNo', 'ItemCode']。然后我們將代碼和類別與“-”連接在一起,并對發票進行分組以創建完整的字符串。


df1 = df.groupby(['InvoiceNo', 'ItemCode'])['Qty'].sum().reset_index('ItemCode')


df1 = df1['ItemCode'].str.cat(df1['Qty'].astype(str), '-').groupby(level=0).agg(', '.join)


#InvoiceNo

#Inv-001         a-1, b-2, c-1

#Inv-002    a-4, b-1, c-1, d-4

#Inv-003              b-2, e-1

#Name: ItemCode, dtype: object

你會注意到我不需要整理任何東西。這是因為groupby默認情況下對分組鍵進行排序,所以在第一行之后系列保證按 排序['InvoiceNo', 'ItemCode'],這是我們之前想要的', '.join


查看完整回答
反對 回復 2023-03-30
?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

干得好:


df1 = df.groupby(['InvoiceNo', 'ItemCode'], sort=False).Qty.sum().reset_index()

df1['Desired result'] = df1.ItemCode + '-' + df1.Qty.astype(str)

print(df1.groupby(['InvoiceNo'])['Desired result'].apply(lambda res: ', '.join(sorted(res))).reset_index())

輸出:


  InvoiceNo      Desired result

0   Inv-001       a-1, b-2, c-1

1   Inv-002  a-4, b-1, c-1, d-4

2   Inv-003            b-2, e-1


查看完整回答
反對 回復 2023-03-30
  • 2 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號