Parseltongue
Parseltongue

Reputation: 11657

Pandas- Create a new column filled with the number of observations in another column

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?

Upvotes: 2

Views: 1980

Answers (1)

Rutger Kassies
Rutger Kassies

Reputation: 64443

A simple groupby+transform would work:

df['num_totals'] = df.groupby('ID').transform('count')

Upvotes: 5

Related Questions