Pandas Dataframe: how to add column with number of occurrences in other column

前端 未结 3 1702
梦毁少年i
梦毁少年i 2020-12-17 00:17

I have to following df:

Col1    Col2
test    Something
test2   Something
test3   Something
test    Something
test2   Something
test5   Something
3条回答
  •  情歌与酒
    2020-12-17 00:38

    I can't get the other answers to work when I want to retain more columns than just the two columns Col1 and Col2. Below works well for me with any number of other columns retained.

    df['Occur'] = df['Col1'].apply(lambda x: (df['Col1'] == x).sum())
    

提交回复
热议问题