Pandas Series.str.count() Method



The Series.str.count() method in Pandas allows you to efficiently count the occurrences of a specified regex pattern within each string element of a Series or Index.

This method returns a Series or Index containing the number, representing how many times a specified regular expression is found in each string. This method is useful for analyzing and summarizing text data.

Syntax

Following is the syntax of the Pandas Series.str.count() method −

Series.str.count(pat, flags=0, **kwargs)

Parameters

The Series.str.count() method accepts the following parameters −

  • pat − A string representing the valid regular expression pattern to count.

  • flags − An integer value for regex flags from the re module. Default is 0 (no flags).

  • **kwargs − Additional arguments for compatibility with other string methods. Not used.

Return Value

The Series.str.count() method returns a Series or Index of the same type as the calling object, containing the integer counts of the pattern occurrences.

Example

In this example, we demonstrate the basic usage of the Series.str.count() method by counting the occurrences of the pattern 'a' in each string of a Series.

import pandas as pd
import numpy as np

# Create a Series of strings
s = pd.Series(['Tutorialspoint', 'python', 'pandas', 'program', np.nan, 'example'])

# Count occurrences of 'a' in each string
result = s.str.count('a')

print("Input Series:")
print(s)
print("\nSeries after calling str.count('a'):")
print(result)

When we run the above code, it produces the following output −

Input Series:
0    Tutorialspoint
1            python
2            pandas
3           program
4               NaN
5           example
dtype: object

Series after calling str.count('a'):
0    1.0
1    0.0
2    2.0
3    1.0
4    NaN
5    1.0
dtype: float64

Example

This example demonstrates how to use the Series.str.count() method to count the occurrences of the pattern 'he|wo' in each string of a DataFrame column.

import pandas as pd

# Create a DataFrame with a column of strings
df = pd.DataFrame(['Python', 'pandas', 'tutorialspoint'], columns=['words'])

# Count occurrences of 't' or 'o' in each string
result = df.words.str.count("t|o")

print("Input DataFrame:")
print(df)
print("\nDataFrame column after calling str.count('t|o'):")
print(result)

Following is the output of the above code −

Input DataFrame:
            words
0          Python
1          pandas
2  tutorialspoint

DataFrame column after calling str.count('t|o'):
0    2
1    0
2    5
Name: words, dtype: int64

Example

In this example, we use the Series.str.count() method to count the occurrences of the dollar sign ('$') in each string of a Series. Note that we need to escape the dollar sign using '\\$' to search for the literal character.

import pandas as pd

# Create a Series of strings
s = pd.Series(['$Python', 'pandas', 'p$yt$h$on', '$$C', 'C$++$', 'Python'])

# Count occurrences of '$' in each string
result = s.str.count('\\$')

print("Input Series:")
print(s)
print("\nSeries after calling str.count('\\$'):")
print(result)

Output of the above code is as follows −

Input Series:
0      $Python
1       pandas
2    p$yt$h$on
3          $$C
4        C$++$
5       Python
dtype: object

Series after calling str.count('\$'):
0    1
1    0
2    3
3    2
4    2
5    0
dtype: int64

Example

In this example, we apply the Series.str.count() method to an Index of a Series object to count the occurrences of the pattern 'a' in each string.

import pandas as pd
import numpy as np

# Create a Series of strings
s1 = pd.Series([1, 2, 3, 4], 
   index=['Pandas', 'Python', 'Tutorialspoint', 'count'])
   
# Count occurrences of 'a' in each string
result = s1.index.str.count('a')

print("Input Series:")
print(s1)
print("\nResult after calling str.count('a'):")
print(result)

Output of the above code is as follows −

Input Series:
Pandas            1
Python            2
Tutorialspoint    3
count             4
dtype: int64

Result after calling str.count('a'):
Index([2, 0, 1, 0], dtype='int64')
python_pandas_working_with_text_data.htm
Advertisements