0

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]})

enter image description here

I want to put the same row together like below

Is it possible?

enter image description here

ghost_like
  • 167
  • 8

2 Answers2

3

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
Corralien
  • 109,409
  • 8
  • 28
  • 52
1

It should hopefully work with the following code.

grouped = df.groupby(['model', 'lot'])['qty'].sum().reset_index()
grouped
Ricca
  • 73
  • 5