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

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

    is that what you want:

    import numpy as np
    import pandas as pd
    
    # generating random DataFrame
    brands_list = ['brand{}'.format(i) for i in range(10)]
    a = pd.DataFrame({'brands': np.random.choice(brands_list, 100)})
    b = pd.DataFrame(np.random.randint(0,10,size=(100, 3)), columns=list('ABC'))
    df = pd.concat([a, b], axis=1)
    print(df.head())
    
    # generate 'brands' DF
    brands = pd.DataFrame(df.brands.value_counts().reset_index())
    brands.columns = ['brands', 'count']
    print(brands)
    
    # merge 'df' & 'brands_count'
    merged = pd.merge(df, brands, on='brands')
    print(merged)
    

    PS first big part is just a dataframe generation.

    The part which is interesting for you starts with the # generate 'brands' DF comment

提交回复
热议问题