Skip to content Skip to sidebar Skip to footer

Creating One Dataframe From Another (using Pivot)

I'm having a problem with pandas. I have a dataframe with three columns: 'id1','id2','amount'. From this, I would like to create another dataframe which index is 'id1', which colum

Solution 1:

I think you can use pivot with rename_axis (new in pandas 0.18.0):

print df
             id1          id2  amount
0   first_person   first_item      10
1   first_person  second_item       6
2  second_person   first_item      18
3  second_person  second_item      36

print df.pivot(index='id1', columns='id2', values='amount')
        .rename_axis(None)
        .rename_axis(None, axis=1)

               first_item  second_item
first_person           10            6
second_person          18           36

Post a Comment for "Creating One Dataframe From Another (using Pivot)"