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.>
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