2

I have a DataFrame object df. One of the column values in df is ID There are many rows with the same ID.

I want to create a new columns num_totals that counts the number of observation for each ID. For example, something like this:

ID | Num Totals
1  |    3
1  |    3
1  |    3
2  |    2
2  |    2
3  |    3
3  |    3
3  |    3
4  |    1

What's the fastest way to do this in Pandas?

Parseltongue
  • 11,157
  • 30
  • 95
  • 160

1 Answers1

5

A simple groupby+transform would work:

df['num_totals'] = df.groupby('ID').transform('count')
Rutger Kassies
  • 61,630
  • 17
  • 112
  • 97