简体   繁体   中英

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?

在此处输入图片说明

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM