Jia*_*ang 0 python numpy dataframe pandas
I tried to split words then count them by using python pandas.
The original data is like,
col_A
happy, not happy
sad,happy
sad, happy
angry, happy
angry, sad
Run Code Online (Sandbox Code Playgroud)
I tried using this function to count the words in col_A.
word_list= df.col_A.apply(lambda x: pd.value_counts(x.split(","))).sum(axis=0)
word_list.sort_values(ascending = False)
Run Code Online (Sandbox Code Playgroud)
It will give me the results like,
angry 2
happy 2
sad 2
happy 2
not happy 1
sad 1
Run Code Online (Sandbox Code Playgroud)
How can I avoid these blanks to return the real counts of values?
I want to return a list like,
happy 4
sad 3
angry 2
not happy 1
Run Code Online (Sandbox Code Playgroud)
Here is a solution which is very similar to @anky_91's one:
In [96]: df.col_A.str.replace(r"\s*,\s*", ",").str.get_dummies(",").sum()
Out[96]:
angry 2
happy 4
not happy 1
sad 3
dtype: int64
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
90 次 |
| 最近记录: |