0

I have the following Pandas dataframe:

import pandas

df = pandas.DataFrame({'Status': ['Draft', 
   'Active','Completed','Completed', 'Draft', 'Active'], 'Region': 
   ['Europe','Europe', 'Asia','Australia', 'Asia', 'Australia'],'Budget': [1000,
   2000,3000,4000,5000,6000]})

I am grouping and aggregating it as follows:

grouped_by_region = df.groupby('Region').agg({'Budget':sum})

So the output of print(grouped_by_region) is:

Region    Budget

Asia         8000
Australia   10000
Europe       3000

What I want to do is split the budget column into "Budget draft", "Budget active", "Budget completed" columns. So basically I need to create new columns based 'Status' column values and rename them accordingly.

How to achieve this?

barciewicz
  • 3,511
  • 6
  • 32
  • 72

1 Answers1

0

IIUC, you can just add Status to your groupby and unstack:

df.groupby(['Region', 'Status']).agg({'Budget':sum}).unstack(fill_value=0)

          Budget                
Status    Active Completed Draft
Region                          
Asia           0      3000  5000
Australia   6000      4000     0
Europe      2000         0  1000

Though the pivot solutions proposed in the comments work just as well

sacuL
  • 49,704
  • 8
  • 81
  • 106