I have the following dataframe:
>>> df
Out[15]:
group type amount number
0 group_A buy 100 123
1 group_A view 0 111
2 group_B view 0 222
3 group_A view 0 222
I'd like to pivot the data so that I end up with:
type group_A group_B
0 amount buy 100 0
1 number buy 0 123
2 number view 333 222
How do I accomplish this?
Pandas DataFrame: transpose() functionThe transpose() function is used to transpose index and columns. Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. If True, the underlying data is copied. Otherwise (default), no copy is made if possible.
Use the T attribute or the transpose() method to swap (= transpose) the rows and columns of pandas. DataFrame . Neither method changes the original object but returns a new object with the rows and columns swapped (= transposed object).
Pandas DataFrame replace() MethodThe replace() method replaces the specified value with another specified value. The replace() method searches the entire DataFrame and replaces every case of the specified value.
To change the index values we need to use the set_index method which is available in pandas allows specifying the indexes. where, inplace parameter accepts True or False, which specifies that change in index is permanent or temporary. True indicates that change is Permanent.
Using:
df=pd.DataFrame([['group_A','buy',100,123],['group_A','view',0,111],['group_B','view',0,222],['group_A','view',0,222]],columns=['group','type','amount','number'])
First sum the indices and orientate:
>>> df = df.groupby(['type','group']).sum().transpose().stack(0).reset_index()
>>> df
group level_0 type group_A group_B
0 amount buy 100 NaN
1 amount view 0 0
2 number buy 123 NaN
3 number view 333 222
Drop rows that are all zero:
df = df[~((df['group_A']==0) | (df['group_B']==0))]
Fillna's:
>>> df.fillna(0)
group level_0 type group_A group_B
0 amount buy 100 0
2 number buy 123 0
3 number view 333 222
Somewhat guessing in a few place here, but it should give you a start.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With