okl
okl

Reputation: 317

Pandas: how to select row with certain word

How can I display only the row where the text contains the word like * AV * e.g 'AV Snow' or 'AV (Some)' or 'Me AV'

# Select Structural status = 'AVAILABLE' or like *AV*
value_list = ['AVAILABLE', '[AV]']

'[AV]' doesnt seem correct

# Grab DataFrame rows where column has certain values
new_df = df[df.STRUCTURALSTATUS.isin(value_list)]
new_df.shape

Upvotes: 4

Views: 9838

Answers (2)

Alex
Alex

Reputation: 826

If you want to use regex patterns, you might want to use str.contains on your serie to filter the DataFrame. Then you can apply a list with the isin function on your result.

Example:

df[df.STRUCTURALSTATUS.str.contains('[AV]')]

Upvotes: 0

jpp
jpp

Reputation: 164623

Here is one way.

Solution

import pandas as pd

df = pd.DataFrame({'A': ['AV', 'AV Snow', 'Test', 'AV (Some)',
                         'Nothing', 'Me AV', 'Available', 'NA']})

df = df[df['A'].str.contains('AV', regex=False, case=False, na=False)]

Result

           A
0         AV
1    AV Snow
3  AV (Some)
5      Me AV
6  Available

Explanation

  • regex=False disables regex as it is not required for your particular task.
  • case=False makes the search case insensitive.
  • na=False means you won't see errors if there are unusual types in your series, e.g. non-strings.

Upvotes: 13

Related Questions