panda
panda

Reputation: 625

How to filter string in multiple conditions python pandas

I have following dataframe

import pandas as pd
data=['5Star','FiveStar','five star','fiv estar']
data = pd.DataFrame(data,columns=["columnName"])

When I try to filter with one condition it works fine.

data[data['columnName'].str.contains("5")]

Output:

    columnName
0   5Star

But It gives an error when doing with multiple conditions.

How to filter it for conditions five and 5?

Expected Output:

    columnName
0   5Star
2   five star

Upvotes: 6

Views: 10130

Answers (1)

U13-Forward
U13-Forward

Reputation: 71570

Use str.contains with a string with values separated by '|':

print(data[data['columnName'].str.contains("5|five")])

Output:

    columnName
0   5Star
2   five star

Upvotes: 11

Related Questions