本文介绍了在pandas df中提取带有子字符串的行,该子字符串包含空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取:

>>>dft[dft['a'].str.contains('new+')]
a
0   new+folder
1   new+ folder



>>>dft[dft['a'].str.contains('new+ ')]
a



>>>dft[dft['a'].str.contains('new+ f')]
a

如何解决出现时出现的错误后加上 + 还是感觉特殊?

How can I get the error resolved that comes when there is a after a + or I feel special characters?

熊猫0.24.2
Python 3.7.3 64位

Pandas 0.24.2Python 3.7.3 64-bit

推荐答案

是的, + 是特殊的正则表达式字符,如果需要使用 query 的有效解决方案,则需要对其进行转义:

Yes, + is special regex character, need escape it if need working solution with query:

print(dft.query('a.str.contains("new\+ ")', engine='python').head())
               a
1  /new+ folder/

解决方案 regex = False 在这里不起作用:

Solution with regex=False here not working:

print(dft.query('a.str.contains("new+ ", regex=False)', engine='python').head())



如果想要通过同时使用这两种解决方案。

If want filtering by boolean indexing working both solutions.

这篇关于在pandas df中提取带有子字符串的行,该子字符串包含空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:13
查看更多