Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 826
okl Avatar asked Jan 02 '23 17:01

okl


1 Answers

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.
like image 98
jpp Avatar answered Jan 05 '23 07:01

jpp