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

后端 未结 5 860
灰色年华
灰色年华 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:35

    You want to use transform.

    import numpy as np
    import pandas as pd
    
    np.random.seed(0)
    
    # Create dummy data.
    df = pd.DataFrame({'brands': ['brand{0}'.format(n) 
                       for n in np.random.random_integers(0, 5, 10)]})
    
    df['brand_count'] = \
        df.groupby('brands', as_index=False)['brands'].transform(lambda s: s.count())
    
    >>> df
       brands brand_count
    0  brand4           1
    1  brand5           2
    2  brand0           1
    3  brand3           4
    4  brand3           4
    5  brand3           4
    6  brand1           1
    7  brand3           4
    8  brand5           2
    9  brand2           1
    

    For reference:

    >>> df.brands.value_counts()
    brand3    4
    brand5    2
    brand4    1
    brand0    1
    brand1    1
    brand2    1
    Name: brands, dtype: int64
    

提交回复
热议问题