英文:
To put the same rows together in a data frame
问题
Sure, here's the translated code portion:
有一个包含三列的数据框:型号(model)、批次(lot)和数量(qty)
import pandas as pd
df = pd.DataFrame({'model':['A','A','A','B','B','B','C','C','C','C'],
'lot':['x','x','y','y','y','y','y','y','z','z'],
'qty':[1,2,1,3,1,4,3,2,2,2]})
I hope this helps!
英文:
There is one data frame with three columns: model, lot, and qty
import pandas as pd
df = pd.DataFrame({'model':['A','A','A','B', 'B','B','C','C','C','C'],
'lot':['x','x','y','y', 'y','y','y','y','z','z'],
'qty':[1,2,1,3,1,4,3,2,2,2]})
I want to put the same row together like below
Is it possible?
答案1
得分: 3
你可以使用:
df.groupby(['model', 'lot'], as_index=False)['qty'].sum()
model lot qty
0 A x 3
1 A y 1
2 B y 8
3 C y 5
4 C z 4
英文:
You can use:
>>> df.groupby(['model', 'lot'], as_index=False)['qty'].sum()
model lot qty
0 A x 3
1 A y 1
2 B y 8
3 C y 5
4 C z 4
答案2
得分: 1
以下是已翻译的内容:
应该可以与以下代码一起使用。
grouped = df.groupby(['model', 'lot'])['qty'].sum().reset_index()
grouped
英文:
It should hopefully work with the following code.
grouped = df.groupby(['model', 'lot'])['qty'].sum().reset_index()
grouped
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论