在数据帧上找到特定的词

问题描述 投票:0回答:1

[当我在数据框中查找单词时,它会向我显示包含这些字母的每个条目,但我真的希望它向我显示该特定单词。你能帮帮我吗?

这里是示例:

import pandas as pd
d = {'col1': ['ROL', 'ROVER','ROL- 33','ROLLER','ROL -2','TROLLER','rol nº12','rolter','nan'] ,'col2': [1, 2,3,4,5,6,7,9,10]}
df = pd.DataFrame(data=d)  
ROL = df[df['col1'].fillna(0).str.contains("ROL|rol",na=False)] 

输出是这样的

enter image description here

但是我真正想要的是没有这些条目的东西

enter image description here

python pandas dataframe
1个回答
1
投票

首先应用正则表达式来确定要包含的观察值

ids = df.col1.str.contains('rol$|rol-|rol ', flags = re.IGNORECASE, regex = True, na = False)

过滤器:

df[ids]
© www.soinside.com 2019 - 2024. All rights reserved.