Reputation: 107
I have the following example dataframe:
I would like to select only the rows, from the column "" that have the word 'SENSOR', uppercase or lowercase.
import pandas as pd
data = {'first_column': ['SEN_strainG', 'second_valueSEN', 'HHHH', 'sen_precipitacion', 'sen'],
'second_column': [1,2,3,4,5]}
df = pd.DataFrame(data)
I solved the problem with the following code, this one is working perfectly:
df_result = df[(df['first_column'] == 'SEN_strainG') |
(df['first_column'] == 'second_valueSEN') |
(df['first_column'] == 'sen_precipitacion') |
(df['first_column'] == 'sen')]
However, I thought it is possible to generalize the implementation to look for 'sen' or 'SEN'. Is there a way to do this and get the same result?
Upvotes: 1
Views: 2480
Reputation: 13251
df[df.first_column.str.contains('sen', na=False, flags=re.IGNORECASE)]
Output:
first_column second_column
0 SEN_strainG 1
1 second_valueSEN 2
3 sen_precipitacion 4
4 sen 5
Upvotes: 2