Skip to content Skip to sidebar Skip to footer

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 obser

Solution 1:

A simple groupby+transform would work:

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

Post a Comment for "Pandas- Create A New Column Filled With The Number Of Observations In Another Column"