Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas data frame spread function or similar?

Tags:

python

pandas

Here's a pandas df:

df = pd.DataFrame({'First' : ['John', 'Jane', 'Mary'], 
                      'Last' : ['Smith', 'Doe', 'Johnson'], 
                      'Group' : ['A', 'B', 'A'], 
                      'Measure' : [2, 11, 1]})

df
Out[38]: 
  First     Last Group  Measure
0  John    Smith     A        2
1  Jane      Doe     B       11
2  Mary  Johnson     A        1

I would like to "spread" the Group variable with the values in Measure.

df_desired
Out[39]: 
  First     Last  A   B
0  John    Smith  2   0
1  Jane      Doe  0  11
2  Mary  Johnson  1   0

Each level within Group variable becomes its own column populated with the values contained in column Measure. How can I achieve this?

like image 625
Doug Fir Avatar asked Dec 23 '22 05:12

Doug Fir


1 Answers

Using pivot_table

df.pivot_table(index=['First','Last'],columns='Group',values='Measure',fill_value=0)
Out[247]: 
Group          A   B
First Last          
Jane  Doe      0  11
John  Smith    2   0
Mary  Johnson  1   0
like image 120
BENY Avatar answered Dec 25 '22 20:12

BENY