To use the str.contains()
method in Pandas, you can use the str
attribute of a Pandas series, followed by the contains()
method.
This method allows you to check if a string contains a specified substring, and returns a boolean series indicating whether each string in the series contains the substring.
Here's an example of using the str.contains()
method in Pandas:
import pandas as pd
# create a sample series
s = pd.Series(["apple", "banana", "orange", "pear"])
# check if each string in the series contains the substring "an"
s_contains = s.str.contains("an")
# display the result
print(s_contains)
This will check if each string in the series contains the substring "an", and return a boolean series indicating whether each string in the series contains the substring.
The output will be:
0 False
1 True
2 False
3 False
dtype: bool
The str.contains()
method is a convenient way to check if a string contains a specified substring.
It returns a boolean series indicating whether each string in the series contains the substring, which you can use to filter the data in the series.
Related tutorials curated for you
How to convert string to float in Pandas
How to use Timedelta in Pandas
How to use intertuples() in Pandas
How to calculate the variance in Pandas DataFrame
How to calculate the standard deviation in Pandas DataFrame
How to apply a function to multiple columns in Pandas
How to get the number of columns in a Pandas DataFrame
How to stack two Pandas DataFrames
How to get the absolute value for a column in Pandas
How to convert a series to a NumPy array in Pandas
How to convert a Pandas Index to a List
How to sort by two columns in Pandas