How to filter string in multiple conditions python pandas

≯℡__Kan透↙ 提交于 2021-02-08 03:31:57

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/53812967/how-to-filter-string-in-multiple-conditions-python-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!