4

I have following dataset.

game_id h_abbr a_abbr 
0001    WSH    TOR 
0002    ANA    TOR 
0003    TOR    MIN

I want to count how many games each team has played so far. Output should be like this

game_id h_abbr a_abbr ht_game_no at_game_no
0001    WSH    TOR    1          1
0002    ANA    TOR    1          2
0003    TOR    MIN    3          1

1 Answer 1

3

Use DataFrame.set_index with DataFrame.stack for MultiIndex Series, then use GroupBy.cumcount for counter, reshape by Series.unstack, add rename and add to original DataFrame by DataFrame.join:

s = df.set_index('game_id')[['h_abbr','a_abbr']].stack()

d = {'h_abbr':'ht_game_no','a_abbr':'at_game_no'}
df = df.join(s.groupby(s).cumcount().add(1).unstack().rename(columns=d), on='game_id')
print (df)
   game_id h_abbr a_abbr  ht_game_no  at_game_no
0        1    WSH    TOR           1           1
1        2    ANA    TOR           1           2
2        3    TOR    MIN           3           1
Sign up to request clarification or add additional context in comments.

5 Comments

sorry without description it is not so clear
@BrownBear - I looking for better solution, now add desc.
What's s.groupby(s)? I never saw groupby self before.
@tdy - It is trick for groupby by Series if need grouping by their values.
@tdy - But it is rare use.

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.