Counting Mulitple Values in a Python pandas Dataframe Column
<p>I'm trying to count unique values in a pandas dataframe column that contains multiple values separated by a string. I could do this using value_counts() if this were a series, but how would I do this in a dataframe? It seems like a dataframe should be easier. </p>

<p>Data:</p>

<pre><code>                      ID       Tags
 Created at 
 2016-03-10 09:46:00  3074     tag_a
 2016-04-13 11:50:00  3524     tag_a tag_b 
 2016-05-18 15:22:00  3913     tag_a tag_b tag_c
</code></pre>

<p>Code:</p>

<pre><code>%matplotlib inline
import pandas as pd

# read csv into the data dataframe
allData = r'myData.csv'

tickets_df = pd.read_csv((allData),usecols=['Id','Created at','Tags'],parse_dates=['Created at'], index_col=['Created at'])
tickets_df.fillna(0,inplace=True)
tickets_df['2016':'2016']

# this would work with a series:

tickets_df[tickets_df['Tags'].str.split().apply(lambda x: pd.Series(x).value_counts()).sum()] 
</code></pre>

<p>Error:</p>

<pre><code>KeyError: '[   3.    2.    3.    5.    2.  102.    9.    5.    1.    4.    1.  161.\n    4.    4.    1.    6.    4.   34.    1.    1.    1.    6.    2.    5.\n    1.    1.    1.    1.   11.    2.    1.    1.    3.    1.    1.    1.\n    1.    1.    1.    1.    2.    1.    1.    2.    2.    6.    1.    4.\n    2.    1.    1.    2.    1.    1.    1.    3.    2.    1.    4.   35.\n   11.    2.    1.   13.    3.    8.   63.   87.    2.    2.    1.    1.\n    1.    1.    1.    1.  150.    1.   24.    3.    7.    5.    1.    1.\n    3.    4.    2.    6.    1.    2.    3.    5.    2.    5.   15.    1.\n   42.    1.   14.    1.    1.    1.    6.   13.   13.    9.    2.   11.\n    3.    1.    1.] not in index'
</code></pre>

<p>Desired Output: </p>

<pre><code>tag_a  3
tag_b  2 
tag_c  1 
</code></pre>

<p>Use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.split.html" rel="nofollow"><code>str.split</code></a> with <code>expand=True</code> to separate each string into different columns, then use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.stack.html" rel="nofollow"><code>stack</code></a> followed by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.value_counts.html" rel="nofollow"><code>value_counts</code></a>:</p>

<pre><code>df['Tags'].str.split(expand=True).stack().value_counts()
</code></pre>

<p>The resulting output:</p>

<pre><code>tag_a    3
tag_b    2
tag_c    1
</code></pre>

python pandas