0

Given a DataFrame like this:

n     name  number  time  
0     foo    0       .1
1     foo    3       .15
2     bar    0       .2
3     bar    2       .3
4     foo    1       .4
5     foo    5       .45
6     bar    3       .5
7     bar    4       .55
8     bar    5       .6
9     bar    1       .7

Make this DataFrame:

n     name  number  time    n     name  number  time
0     foo    0       .1     2     bar    0       .2
1     foo    3       .15    6     bar    3       .5
4     foo    1       .4     9     bar    1       .7
5     foo    5       .45    8     bar    5       .6

I've hacked together a solution using using shift that works if the data appears like this:

n     name  number  time  
0     foo    0       .1
1     bar    0       .15
2     foo    1       .2
3     bar    2       .3
4     foo    3       .4
5     bar    5       .5

But I can't guarantee that the original data interleaves 'foo' and 'bar.' I need to be able to get pairs any distance apart.

1 Answer 1

1

IIUC still need groupby then concat

df=pd.concat([y.reset_index(drop=True).set_index('number') for x , y in df.groupby('name')],axis=1, join='inner').reset_index()
Out[322]: 
   number  n name  time  n name  time
0       0  2  bar   0.2  0  foo  0.10
1       3  6  bar   0.5  1  foo  0.15
2       5  8  bar   0.6  5  foo  0.45
3       1  9  bar   0.7  4  foo  0.40
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.