如何在多个条件下过滤字符串 python pandas [英] How to filter string in multiple conditions python pandas

查看:60
本文介绍了如何在多个条件下过滤字符串 python pandas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框

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")]

输出:

    columnName
0   5Star

但是在处理多个条件时会出错.

But It gives an error when doing with multiple conditions.

如何过滤条件55?

预期输出:

    columnName
0   5Star
2   five star

推荐答案

使用 str.contains 和一个字符串,其中的值由 '|' 分隔:

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

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

输出:

    columnName
0   5Star
2   five star

这篇关于如何在多个条件下过滤字符串 python pandas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆