0

I have a pandas dataframe which looks like the following.

df =     index| date | amount| type|
           0  | 2015 | 1000  | A   |
           1  | 2015 | 100   | B   |
           2  | 2016 | 3500  | A   |
           3  | 2017 | 150   | C   |

I want to produce a dataframe with a new column based on types. The result would look like the following.

df =     index| date | A   | B   | C   
           0  | 2015 | 1000| 100 |0
           1  | 2016 | 3500| 0   |0
           2  | 2017 | 0   | 0   |150
tsadkan yitbarek
  • 1,360
  • 2
  • 11
  • 28

1 Answers1

2

you can try

ss=df.groupby(['date','type']).sum().reset_index()
ss.pivot(index='date',columns='type',values='amount').fillna(0)
Sreekiran A R
  • 3,123
  • 2
  • 20
  • 41