xingyuan jiang
xingyuan jiang

Reputation: 153

How do I get the number by pandas

Now,I have this Series:

0    a,b
1    a
2    c
3    b,a,d
4    a,c
dtype: object

so,How can I get the series(I try to use sr.value_counts(),but no valid) like this:

a    4
b    2
c    2
d    1
dtype: int64

thanks

Upvotes: 1

Views: 45

Answers (3)

piRSquared
piRSquared

Reputation: 294488

Option 1

sr.str.split(',', expand=True).stack().value_counts()

Option 2

sr.str.get_dummies(',').sum()

Upvotes: 1

DYZ
DYZ

Reputation: 57085

your_series.str.split(",").apply(pd.Series).stack().value_counts()
#a    4
#b    2
#c    2
#d    1

Upvotes: 1

AChampion
AChampion

Reputation: 30268

You can split and expand the string, e.g.:

>>> sr.str.split(',', expand=True).stack().value_counts()
a    4
b    2
c    2
d    1
dtype: int64

Upvotes: 3

Related Questions