Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas – get rid of repeated pair of column values in a row and move unique row value to new column

I have a dataframe of football matches, like so:

team_id  adversary_id round_id xG
262      263          1        0.45
263      262          1        0.34
245      254          1        0.67
254      245          1        0.15
...

How do I get rid of repeated fixtures and change dataframe into this wider format:

team_id  adversary_id  round_id  xG_team  xG_adversary
262      263           1         0.45     0.34
245      254           1         0.67     0.15
...

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try with numpy sort then merge

df[['team_id','adversary_id']] = np.sort(df[['team_id','adversary_id']].values,axis=1)
out = df.iloc[0::2].merge(df.iloc[1::2],on = ['team_id','adversary_id','round_id'], suffixes = ('_team','_adversary'))
Out[403]: 
   team_id  adversary_id  round_id  xG_team  xG_adversary
0      262           263         1     0.45          0.34
1      245           254         1     0.67          0.15
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading