I have a dataframe which I need to do groupby by multiple columns and get the items within every group as a row;
I need to output a below table after group;
I have been looking for the answer but I was not able to find any, I appreciate if you can help we with the code.
Advertisement
Answer
You could try this:
JavaScript
x
8
1
import pandas as pd
2
3
df = pd.read_csv('data.csv')
4
groups = ['group1', 'group2', 'group3']
5
df = df.groupby(groups).apply(lambda a: a.drop(groups, axis=1)[:])
6
7
print(df)
8
Output:
JavaScript
1
8
1
itemNo itemName
2
group1 group2 group3
3
A AA AAA 0 4412 xxx
4
3 9812 yhk
5
C CC 4 4431 alk
6
B AA BB 1 3456 xsc
7
2 4566 thg
8