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 = ...
key_col = "brand"
count_col = "brand_count"
result = (
df.join(
df[key_col].value_counts().rename(count_col),
how="left",
on=key_col)
)
If you need to join the counts to a different dataframe remember to fill NaN
s with zeros:
df = ...
other = ...
key_col = "brand"
count_col = "brand_count"
result = (
other.join(
df[key_col].value_counts().rename(count_col),
how="left",
on=key_col)
.fillna({count_col: 0})
)