Mariana
Mariana

Reputation: 75

Count times a value of a column appears and add a column to the dataframe with it

I have a dataframe with 4 columns, one of them being people's names and another one activity they practiced. I want that in front of each row appears the number of times that combination appears. All the ways i found of counting change the dataframe or reduce the size of the data frame, apearing each combination only one time. I would like the dataframe to stay the same just with one more column with the number of times that combination exists. Does anyone know a way?

enter image description here

Upvotes: 1

Views: 611

Answers (1)

jpp
jpp

Reputation: 164773

groupby + size

Assuming your grouper columns are 0 and 2:

df['combination_count'] = df.groupby([0, 2])[1].transform('size')

To move the new column to the front:

cols = df.columns.tolist()
cols.insert(0, cols.pop(cols.index('combination_count')))
df = df.reindex(columns=cols)

Upvotes: 1

Related Questions