Python 如何通过正则表达式过滤熊猫中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15325182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:49:44  来源:igfitidea点击:

How to filter rows in pandas by regex

pythonregexpandas

提问by justinvf

I would like to cleanly filter a dataframe using regex on one of the columns.

我想在其中一列上使用正则表达式干净地过滤数据框。

For a contrived example:

对于一个人为的例子:

In [210]: foo = pd.DataFrame({'a' : [1,2,3,4], 'b' : ['hi', 'foo', 'fat', 'cat']})
In [211]: foo
Out[211]: 
   a    b
0  1   hi
1  2  foo
2  3  fat
3  4  cat

I want to filter the rows to those that start with fusing a regex. First go:

我想将行过滤为以f使用正则表达式开头的行。先去:

In [213]: foo.b.str.match('f.*')
Out[213]: 
0    []
1    ()
2    ()
3    []

That's not too terribly useful. However this will get me my boolean index:

这不是太有用。但是,这将使我获得布尔索引:

In [226]: foo.b.str.match('(f.*)').str.len() > 0
Out[226]: 
0    False
1     True
2     True
3    False
Name: b

So I could then do my restriction by:

所以我可以通过以下方式进行限制:

In [229]: foo[foo.b.str.match('(f.*)').str.len() > 0]
Out[229]: 
   a    b
1  2  foo
2  3  fat

That makes me artificially put a group into the regex though, and seems like maybe not the clean way to go. Is there a better way to do this?

尽管如此,这让我人为地将一个组放入正则表达式中,并且似乎可能不是一个干净的方法。有一个更好的方法吗?

回答by waitingkuo

Use containsinstead:

改用包含

In [10]: df.b.str.contains('^f')
Out[10]: 
0    False
1     True
2     True
3    False
Name: b, dtype: bool

回答by lakshman senathirajah

Multiple column search with dataframe:

使用数据框进行多列搜索:

frame[frame.filename.str.match('*.'+MetaData+'.*') & frame.file_path.str.match('C:\test\test.txt')]

回答by Michael Siler

This may be a bit late, but this is now easier to do in Pandas. You can call match with as_indexer=Trueto get boolean results. This is documented (along with the difference between matchand contains) here.

这可能有点晚了,但现在在 Pandas 中更容易做到。您可以调用 matchas_indexer=True以获取布尔结果。这是记录(与之间的差异沿matchcontains在这里

回答by Erkan ?irin

There is already a string handling function Series.str.startswith(). You should try foo[foo.b.str.startswith('f')].

已经有一个字符串处理函数Series.str.startswith()。你应该试试foo[foo.b.str.startswith('f')]

Result:

结果:

    a   b
1   2   foo
2   3   fat

I think what you expect.

我想你所期望的。

Alternatively you can use contains with regex option. For example:

或者,您可以使用带有正则表达式选项的 contains。例如:

foo[foo.b.str.contains('oo', regex= True, na=False)]

Result:

结果:

    a   b
1   2   foo

na=Falseis to prevent Errors in case there is nan, null etc. values

na=False是为了防止出现 nan、null 等值时的错误

回答by user3136169

Write a Boolean function that checks the regex and use apply on the column

编写一个布尔函数来检查正则表达式并在列上使用 apply

foo[foo['b'].apply(regex_function)]

回答by sparrow

Thanks for the great answer @user3136169, here is an example of how that might be done also removing NoneType values.

感谢@user3136169 的精彩回答,这里有一个示例,说明如何删除 NoneType 值。

def regex_filter(val):
    if val:
        mo = re.search(regex,val)
        if mo:
            return True
        else:
            return False
    else:
        return False

df_filtered = df[df['col'].apply(regex_filter)]

Also you can also add regex as an arg:

你也可以添加正则表达式作为参数:

def regex_filter(val,myregex):
    ...

df_filtered = df[df['col'].apply(res_regex_filter,regex=myregex)]

回答by YOBEN_S

Using strslice

使用str切片

foo[foo.b.str[0]=='f']
Out[18]: 
   a    b
1  2  foo
2  3  fat