Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transposing part of a pandas dataframe

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?

like image 215
greenafrican Avatar asked Feb 17 '14 18:02

greenafrican


People also ask

How do you transpose a DataFrame in pandas?

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.

How do I transpose columns in pandas?

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).

How do I change a specific value in pandas?

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.

How do I change the index of a data frame?

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.


1 Answers

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.

like image 163
Daniel Avatar answered Oct 04 '22 03:10

Daniel