gpt4 book ai didi

regex - 根据列中的字符串过滤数据框

转载 作者:行者123 更新时间:2023-12-04 01:40:33 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





pandas filter rows by two column values with case insenstive

(4 个回答)



Filtering pandas dataframe rows by contains str

(1 个回答)



how to filter pandas dataframe by string?

(3 个回答)


2年前关闭。




所以为了简单起见,因为我的数据集非常大,假设我有一个数据框:

df = pd.DataFrame([['Foo', 'Foo1'], ['Bar', 'Bar2'], ['FooBar', 'FooBar3']],
columns= ['Col_A', 'Col_B'])

当指定的列行包含部分不区分大小写的字符串 (foo) 时,我需要以消除整行的方式过滤此数据框。在这种情况下,我尝试过无济于事...... PS,我的正则表达式技能很垃圾,所以如果它因为这个原因不起作用,请原谅我。
df = df[df['Col_A'] != '^[Ff][Oo][Oo].*']

由于我的数据集的大小,效率是一个问题,这就是我没有选择迭代路线的原因。提前致谢。

最佳答案

使用 str.match

df[~df['Col_A'].str.match('^[Ff][Oo][Oo].*')]

结果
    Col_A   Col_B
1 Bar Bar2

关于regex - 根据列中的字符串过滤数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57599863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com