Reputation: 13
I have the following data in terms of dataframe
data = pd.DataFrame({'colA': ['a', 'c', 'a', 'e', 'c', 'c'], 'colB': ['b', 'd', 'b', 'f', 'd', 'd'], 'colC':['SD100', 'SD200', 'SD300', 'SD400', 'SD500', 'SD600']})
I want the output as attached [enter image description here][2]
I want to achieve this using pandas dataframe in python Can somebody help me?
Upvotes: 1
Views: 1861
Reputation: 32
This will update your data into what you wished
data=data.groupby(['colA','colB']).agg(list)
Upvotes: 0
Reputation: 494
I don't know why you want to make multindex, but you can simply sort_values
or use groupby
.
import pandas as pd
df = pd.DataFrame({"ColumnA":['a','c','a','e','c','c'],
"ColumnB":['b','d','b','f','d','d'],
"ColumnC":['SD100','SD200','SD300','SD400','SD500','SD600']})
print(df)
ColumnA ColumnB ColumnC
0 a b SD100
1 c d SD200
2 a b SD300
3 e f SD400
4 c d SD500
5 c d SD600
df = df.sort_values(by=['ColumnA','ColumnB'])
df.set_index(['ColumnA', 'ColumnB','ColumnC'], inplace=True)
df
Upvotes: 0
Reputation: 5757
You can try:
Column A Column B Column C
0 a b SD100
1 c d SD200
2 a b SD300
3 e f SD400
4 c d SD500
5 c d SD600
>>> df.groupby(['Column A', 'Column B']).agg(list)
Column C
Column A Column B
a b [SD100, SD300]
c d [SD200, SD500, SD600]
e f [SD400]
Upvotes: 2