How to merge pandas value_counts() to dataframe or use it to subset a dataframe
<p>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.</p>

<pre><code> df has many columns including one named 'brands'
 brands = df.brands.value_counts()

 brand1   143
 brand2   21
 brand3   101
 etc.
</code></pre>

<p>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"?</p>

<p>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:</p>

<pre><code> df2 = pd.DataFrame({'brands': list(brands_all[0]), "brand_count":
 list(brands_all[1])})
 (merge with df)
</code></pre>

<p>Ultimately, I want to obtain this:</p>

<pre><code> col1  col2  col3  brands  brand_count ... col150
                   A        30
                   C        140
                   A        30
                   B        111 
</code></pre>