import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex']}
df = pd.DataFrame(data=my_dict)
print(df.NAME.str.count(pat='a'))
Output
0 1
1 1
2 0
Here we have searched for a and returned number of occurences.
import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex']}
df = pd.DataFrame(data=my_dict)
df['number']=df.NAME.str.count(pat='a')
print(df)
Output
NAME number
0 Ravi 1
1 Raju 1
2 Alex 0
import pandas as pd
import re
my_dict={'NAME':['Ravi','Raju','Alex']}
df = pd.DataFrame(data=my_dict)
df['number']=df.NAME.str.count('r', re.I)
print(df)
Output
NAME number
0 Ravi 1
1 Raju 1
2 Alex 0
In above code we have included re module and this is a case insensitive pattern matching.
import pandas as pd
my_dict={'NAME':['Ravi$a','Raj$u$b','Alex']}
df = pd.DataFrame(data=my_dict)
df['number']=df.NAME.str.count(pat='\$')
print(df)
Output
NAME number
0 Ravi$a 1
1 Raj$u$b 2
2 Alex 0
import pandas as pd
df = pd.DataFrame(['John','Mac','Arnold','Krish','Roni'],
columns=['Names'])
df.Names.str.count('r')
Output is here
0 0
1 0
2 1
3 1
4 0
We will use sum() to get total matching substrings.
df.Names.str.count('r').sum()
Output is
2
Pandas
contains() Converting char case slice()
split()
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.