I have a problem when counting the number of items in a pandas string series when there is no sting in a row.
I´m able to count the number of words when there are one ore more items per row. But, if the row has no value (it´s an empty string when running pd.['mytext'].str.split(',')), I´m getting also one.
These answers are not working for me Answer 1 to a solution which gives one for an empty string Answer 2 to a solution which gives one for an empty string.
How can I handle this in a pandas one liner? Thanks in advance.
Taken the example from the first answer:
df = pd.DataFrame(['one apple','','box of oranges','pile of fruits outside', 'one banana', 'fruits'])
df.columns = ['fruits']
The verified answer was
count = df['fruits'].str.split().apply(len).value_counts()
count.index = count.index.astype(str) + ' words:'
count.sort_index(inplace=True)
count
Which gives
Out[13]:
0 words: 1
1 words: 1
2 words: 2
3 words: 1
4 words: 1
Name: fruits, dtype: int64
I want a zero for the second string but every solution tried gave me a one.