How to merge pandas value_counts() to dataframe or use it to subset a dataframe

后端 未结 5 869
灰色年华
灰色年华 2021-02-05 23:53

I used pandas df.value_counts() to find the number of occurrences of particular brands. I want to merge those value counts with the respective brands in the initial dataframe.

5条回答
  •  广开言路
    2021-02-06 00:34

    i think the best way is to use map

    df['brand_count']= df.brand.map(df.brand.value_counts())
    

    this is so much faster than groupby method for example (factor 500 on a 15000 row df) and take only one line

提交回复
热议问题