close

How to filter rows containing a string pattern from a Pandas dataframe

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to filter rows containing a string pattern from a Pandas dataframe in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to filter rows containing a string pattern from a Pandas dataframe?

  1. How to filter rows containing a string pattern from a Pandas dataframe?

    In [3]: df[df['ids'].str.contains("ball")] Out[3]: ids vals 0 aball 1 1 bball 2 3 fball 4

  2. filter rows containing a string pattern from a Pandas dataframe

    In [3]: df[df['ids'].str.contains("ball")] Out[3]: ids vals 0 aball 1 1 bball 2 3 fball 4

Method 1

In [3]: df[df['ids'].str.contains("ball")]
Out[3]:
     ids  vals
0  aball     1
1  bball     2
3  fball     4

Method 2

df[df['ids'].str.contains('ball', na = False)] # valid for (at least) pandas version 0.17.1

Step-by-step explanation (from inner to outer):

  • df['ids'] selects the ids column of the data frame (technically, the object df['ids'] is of type pandas.Series)
  • df['ids'].str allows us to apply vectorized string methods (e.g., lowercontains) to the Series
  • df['ids'].str.contains('ball') checks each element of the Series as to whether the element value has the string ‘ball’ as a substring. The result is a Series of Booleans indicating True or False about the existence of a ‘ball’ substring.
  • df[df['ids'].str.contains('ball')] applies the Boolean ‘mask’ to the dataframe and returns a view containing appropriate records.
  • na = False removes NA / NaN values from consideration; otherwise a ValueError may be returned.

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read