Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Combining two rows in the same dataframe

Tags:

python

pandas

I have a dataframe containing NBA statistics for every game in the 2020 season. The problem is the date frame has 2 rows for every game. 1 row is the stats of one team the 2nd row is the stats for the opposing team. I want to combine these 2 rows. When I combining these I want to double the number of columns and add an "a" in front of all the column names to distinguish which stats are for the home team and which are for the away team.

For example a dataframe that looks like this

Team_ID     Abrv      Game_ID      Pts
123         LAL         111         101
124         NOP         111         110
125         NYK         112         98
126         WAS         112         116

would look like this afterwards:

Team_ID    Abrv       Game_ID      Pts   aTEAM_ID    aAbrv    aPTS
123        LAL        111          101   124         NOP      110
123        NYK        112          98    126         WAS      116
like image 889
brwnsfan Avatar asked Jan 24 '26 05:01

brwnsfan


1 Answers

Try:

cumcount = df.groupby('Game_ID').cumcount()
renamer = {0: 'Home', 1: 'Away'}
df = df.set_index(['Game_ID', cumcount]).rename(renamer, level=1).unstack()

        Team_ID      Abrv       Pts     
           Away Home Away Home Away Home
Game_ID                                 
111         124  123  NOP  LAL  110  101
112         126  125  WAS  NYK  116   98

Slight variation

cumcount = df.groupby('Game_ID').cumcount()
renamer = {0: 'Home', 1: 'Away'}
df = df.set_index(['Game_ID', cumcount]).rename(renamer, level=1) \
  .unstack().swaplevel(0, 1, 1).sort_index(1)

        Away              Home             
        Abrv  Pts Team_ID Abrv  Pts Team_ID
Game_ID                                    
111      NOP  110     124  LAL  101     123
112      WAS  116     126  NYK   98     125
like image 59
piRSquared Avatar answered Jan 25 '26 18:01

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!