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.
df has many columns including one named 'brands'
brands = df.brands.value_counts()
brand1 143
brand2 21
brand3 101
etc.
How do I merge the value counts with the original dataframe such that each brand's corresponding count is in a new column, say "brand_count"?
Is it possible to assign headers to these columns; the names function won't work with series and I was unable to convert it to a dataframe to possibly merge the data that way. But, value_counts outputs a Series of dtype int64 (brand names should be type string) which means I cannot do the following:
df2 = pd.DataFrame({'brands': list(brands_all[0]), "brand_count":
list(brands_all[1])})
(merge with df)
Ultimately, I want to obtain this:
col1 col2 col3 brands brand_count ... col150
A 30
C 140
A 30
B 111