0

I have in pandas by using of groupby() next output (A,B,C are the columns in the input table)

                 C
A    B        
0    0           6
     2           1
     6           5
.    .           .  

Output details: [244 rows x 1 columns] I just want to have all 3 columns instead of one,how is it possible to do?

Output, which I wish:

   A     B      C
   0     0      6
   0     2      1
   .     .      .
Guforu
  • 3,835
  • 8
  • 33
  • 52

1 Answers1

1

It appears to be undocumented, but simply: gb.bfill(), see this example:

In [68]:

df=pd.DataFrame({'A':[0,0,0,0,0,0,0,0],
                 'B':[0,0,0,0,1,1,1,1],
                 'C':[1,2,3,4,1,2,3,4],})
In [69]:

gb=df.groupby(['A', 'B'])
In [70]:

print gb.bfill()
   A  B  C
0  0  0  1
1  0  0  2
2  0  0  3
3  0  0  4
4  0  1  1
5  0  1  2
6  0  1  3
7  0  1  4

[8 rows x 3 columns]

But I don't see why you need to do that, don't you end up with the original DataFrame (only maybe rearranged)?

CT Zhu
  • 52,648
  • 17
  • 120
  • 133
  • Thanks and sorry for a late respond (vacation:-). I have three columns of parameters, the first two columns are independent (for example readers and books), the last column is just pointer, that user read this book. It is possible that some user read some book several times, every attemp is saved is unique line. What I did is some kind of ranking, I group user and books by unique values and summarize the number of pointers. I can do it with the function 'groupby(['User', 'Book']).sum()'. Unfortunately, I have only one column now and want get my table in new format back. – Guforu Apr 22 '14 at 07:15
  • Actually, everything, what I want is described here: http://stackoverflow.com/questions/20181456/sum-up-column-values-in-pandas-dataframe Function as_index=False is important. Thank you very much! – Guforu Apr 22 '14 at 08:28