How to get the result showing in picture 2 from the data shown in picture 1 using python pandas.?
Asked
Active
Viewed 290 times
1 Answers
1
You may be looking for something like this.
import pandas as pd
df = pd.DataFrame({
'Category': ['Fruit','Vegetable','Vegetable','Fruit','Vegetable','Vegetable','Fruit','Vegetable','Fruit','Vegetable'],
'SubCategories': ['Apple','Brinjal','Brinjal','Apple','Carrot','Potato','Apple','Carrot','Banana','Brinjal'],
'Count': [2,1,1,1,3,1,1,2,1,1],
})
df.set_index(['Category','SubCategories']).groupby(level=[0,1]).sum()
Note that you can use df = pd.read_csv()
if you are reading an external csv file. For information on this, see this question.

BoomBoxBoy
- 1,770
- 1
- 5
- 23