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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With