I have a pandas datafram
df
that contains a column
say x
, and I would like to create another column out of x
which is a value_count
of each item in x
.
Here is my approach
x_counts= []
for item in df['x']:
item_count = len(df[df['x']==item])
x_counts.append(item_count)
df['x_count'] = x_counts
This works but this is far inefficient. I am looking for a more efficient way to handle this. Your approach and recommendations are highly appreciated