如何使用正则表达式删除python pandas DataFrame中的行? [英] How to delete rows in python pandas DataFrame using regular expressions?
本文介绍了如何使用正则表达式删除python pandas DataFrame中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个模式:
patternDel = "( \\((MoM|QoQ)\\))";
我想删除列df['Event Name']
与此模式匹配的pandas数据框中的所有行.哪个是最好的方法?数据框中有超过10万行.
And I want to delete all rows in pandas dataframe where column df['Event Name']
matches this pattern. Which is the best way to do it? There are more than 100k rows in dataframe.
推荐答案
str.contains() returns a Series of booleans that we can use to index our frame
patternDel = "( \\((MoM|QoQ)\\))"
filter = df['Event Name'].str.contains(patternDel)
我倾向于保留我们想要的东西,而不是删除行.由于过滤器代表我们要删除的内容,因此我们使用~
来获取所有不匹配的行并将其保留
I tend to keep the things we want as opposed to delete rows. Since filter represents things we want to delete we use ~
to get all the rows that don't match and keep them
df = df[~filter]
这篇关于如何使用正则表达式删除python pandas DataFrame中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文