dataframe string find count 1

dataframe string find count

In [21]: df = pd.DataFrame(['hello', 'world', 'hehe'], columns=['words'])

In [22]: df.words.str.count("he|wo")
Out[22]:
0    1
1    1
2    2
Name: words, dtype: int64

In [23]: df.words.str.count("he|wo").sum()
Out[23]: 4

Here is what the above code is Doing:
1. Create a dataframe with a single column called ‘words’
2. Use the str.count() method to count the number of times ‘he’ or ‘wo’ appears in each row
3. Use the sum() method to sum up the number of times ‘he’ or ‘wo’ appears in the entire dataframe

Similar Posts