Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas value_counts() with multiple values in list form?

I'm trying to do a value_count for a specific column in my dataframe

For example:

    <Fruit>
0   'apple'
1   'apple, orange'
2   'orange'

How do I sum it so it will count it even if it is in a list? So the above should give me:

'Apple'   2
'Orange'  2

I tried turning the string into a list, but not sure how to value_count over fields with a list of values.

like image 679
user3747200 Avatar asked Sep 30 '14 16:09

user3747200


1 Answers

This is a pandonic way

In [8]: s
Out[8]: 
0            apple
1    apple, orange
2           orange
dtype: object

Split the strings by their separators, turn them into Series and count them.

In [9]: s.str.split(',\s+').apply(lambda x: Series(x).value_counts()).sum()
Out[9]: 
apple     2
orange    2
dtype: float64
like image 103
Jeff Avatar answered Sep 22 '22 01:09

Jeff