Skip to content Skip to sidebar Skip to footer

Pivot Table(?) With A Pandas Dataframe

I have a DataFrame that is something similar to this id name value a Adam 5 b Eve 6 c Adam 4 a Eve 3 d Seth 2 b Adam 4 a Adam

Solution 1:

is that what you want?

In [54]: df.pivot_table(index='id', columns='name', values='value', aggfunc='sum')
Out[54]:
name  Adam  Eve  Seth
id
a      7.0  3.0   NaN
b      4.0  6.0   NaN
c      4.0  NaN   NaN
d      NaN  NaN   2.0

or without NaN's:

In [56]: df.pivot_table(index='id', columns='name', values='value', aggfunc='sum', fill_value=0)
Out[56]:
name  Adam  Eve  Seth
id
a        7    3     0
b        4    6     0
c        4    0     0
d        0    0     2

Post a Comment for "Pivot Table(?) With A Pandas Dataframe"