Oracy Martos
Oracy Martos

Reputation: 457

pandas groupby column then create two other columns based on third column

I am trying to group a dataframe by a column and get the total "Approved" or "Not Approved" values based on this grouping, but with no success.

Example:

PlaceTest | Approved    
       21 |        1    
       21 |        0    
       22 |        1    
       22 |        0

My desired output:

PlaceTest | Approved | NotApproved    
       21 |        1 |           1    
       22 |        1 |           1

Thanks!

Upvotes: 2

Views: 391

Answers (2)

Karn Kumar
Karn Kumar

Reputation: 8826

You can achieve this slightly different way, which is two line process though.

$ df
   Approved  PlaceTest
0         1         21
1         0         21
2         1         22
3         0         22

First groupby and sum with a new column:

$ df['Not Approved'] = df.groupby('PlaceTest')['Approved'].sum()

lastly again groupby with fillna

$ df = df.groupby('PlaceTest')['Approved', 'Not Approved'].sum().fillna("1").reset_index()

$ df
   PlaceTest  Approved Not Approved
0         21         1            1
1         22         1            1

Note : Its working with pandas version 0.21.0.

For version greater than 0.21.0 should use as follows for the above use case:

>>> df.groupby('PlaceTest')['Approved', 'Not Approved'].max().fillna("1").reset_index()
   PlaceTest  Approved Not Approved
0         21         1            1
1         22         1            1

Upvotes: 0

timgeb
timgeb

Reputation: 78750

Given

>>> df
   PlaceTest  Approved
0         21         1
1         21         0
2         22         1
3         22         0

you can issue

>>> df.assign(NotApproved=1 - df['Approved']).groupby('PlaceTest').sum().reset_index()
   PlaceTest  Approved  NotApproved
0         21         1            1
1         22         1            1

Upvotes: 1

Related Questions