2

I have a pandas dataframe like following

   mp  me        rt                                                         
0  0   1   1.987366                                                         
1  1   1   1.769593                                                         
2  2   1   1.416274                                                         
3  3   1   1.650428                                                         
4  4   1   1.882780                                                         
5  0   2   1.955086                                                         
6  1   2   1.729387                                                         
7  2   2   1.490797                                                         
8  3   2   1.546333                                                         
9  4   2   1.933006                 

I'd like to generate a new dataframe as folloing

   mp    me=1       me=2                                                    
0  0   1.987366    1.955086                                                 
1  1   1.769593    1.729387                                                 
2  2   1.416274    1.490797                                                 
3  3   1.650428    1.546333                                                 
4  4   1.882780    1.933006                                                 

I tried to use a for loop but unsuccessful.

for j in range(1,3):
    f[str(j)] = pd.DataFrame(a[a['me']==j]['rt'])

Any idea how to do it and effectively?

Thanks. Dan

1 Answer 1

6
In [6]: df.pivot_table(values='rt', rows='mp', cols='me', aggfunc=sum)
Out[6]: 
me         1         2
mp                    
0   1.987366  1.955086
1   1.769593  1.729387
2   1.416274  1.490797
3   1.650428  1.546333
4   1.882780  1.933006
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.