Word Count Of Single Column In Pandas Dataframe
Here is my attempt at a word count for a single column using group by with pandas : First setup the data : columns = ['col1','col2','col3'] data = np.array([['word1','word2','wor
Solution 1:
If I understand you correctly, I think this is what you're looking for:
print(to_count.groupby('col1')['col1'].count())
Output:
col1
word1 2
word3 1
Solution 2:
You can apply value_counts() fn to one column of dataframe. Following applies it all columns one by one:
for onecol in to_count:
print(onecol, ":\n", to_count[onecol].value_counts())
Output:
col1 :word12word31Name:col1,dtype:int64col2 :word51word21word71Name:col2,dtype:int64col3 :word33Name:col3,dtype:int64
Solution 3:
How about this:
Single column:
df['col1'].value_counts()
will return:
word1 2
word3 1
All columns:
df.apply(lambda col: col.value_counts()).fillna(0).astype(int)
will return:
col1 col2 col3
word1 2 0 0
word2 0 1 0
word3 1 0 3
word5 0 1 0
word7 0 1 0
Copy & paste example:
from io import StringIO
import pandas as pd
data = """
col1 col2 col3
0 word1 word2 word3
1 word1 word5 word3
2 word3 word7 word3
"""
df = pd.read_table(StringIO(data), sep='\s+')
print(df['col1'].value_counts())
print(df.apply(lambda col: col.value_counts().astype(int)).fillna(0).astype(int))
Post a Comment for "Word Count Of Single Column In Pandas Dataframe"