Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在标题中使用正则表达式筛选列_Python_Regex_Pandas - Fatal编程技术网

Python 在标题中使用正则表达式筛选列

Python 在标题中使用正则表达式筛选列,python,regex,pandas,Python,Regex,Pandas,我正在读取Excel文件中的数据框,但其中一个列标题中有大量注释。它在所有文本中有一个关键字“Measure”,它只针对这一个标题。在“contains”中,我将如何过滤在标题中某处仅包含关键字“Measure”的任何标题 下面的代码是基于3个过滤器过滤我的数据帧,但第三个过滤器我只想让它识别包含文本“measure”的列本身,而不是将其写为“hereisalltherandomTextMeasure” filtered = df[(df['Mode'].isin(mode_filter)) &

我正在读取Excel文件中的数据框,但其中一个列标题中有大量注释。它在所有文本中有一个关键字“Measure”,它只针对这一个标题。在“contains”中,我将如何过滤在标题中某处仅包含关键字“Measure”的任何标题

下面的代码是基于3个过滤器过滤我的数据帧,但第三个过滤器我只想让它识别包含文本“measure”的列本身,而不是将其写为“hereisalltherandomTextMeasure”

filtered = df[(df['Mode'].isin(mode_filter)) & (df['Level'].isin(level_filter)) & (df['hereisalltherandomtextmeasure'].isin(measure_filter))]
我之所以尝试这样做,是因为我在多个文件上运行相同的代码,但每个文件的“度量”列都会更改

第一个文件:

Mode | Level | hereisalltherandomtextmeasure
第二个文件:

Mode | Level | hereismorerandomtextmeasure
关于它们唯一的静态特性是它们包含单词measure,因此理想情况下,我希望识别只包含单词measure的列,而不是应用完整的字符串

谢谢。

IIUC然后您可以使用查找匹配字符串是否包含在列中的任何位置:

In [7]:
df = pd.DataFrame(columns=['hereisall the random textMeasure', 'Measurement', 'asdasds'])
df.columns[df.columns.str.contains('Measure')]

Out[7]:
Index(['hereisall the random textMeasure', 'Measurement'], dtype='object')

对不起,你在问什么?查找列或筛选列?抱歉,我只想识别其中包含文本“Measure”的列,然后使用.isnin.then just
df.columns[df.columns.str.contains('hereisall the random textmasure')应用筛选度量值
将返回该列我想忽略“Measure”前面的文本,因为这取决于我加载的文件,每次都不同。因此,只要列标题包含“Measure”,那么我的代码就会对其进行过滤。您能提供一些示例字符串以及您想要匹配的内容吗?嗨,Ed,谢谢您的回答。我有以下代码在3个过滤器上过滤我的数据帧:
filtered=df[(df['Mode'].isin(Mode_filter))&(df['Level'].isin(Level_filter))&(df['hereisall the random textmasure'].isin(measure_filter))
。那么,最后一部分如何合并str.contains元素来搜索“measure”?与其发布附加信息的小片段,您是否可以用所有必要的信息编辑您的问题,Q+a站点,而不是论坛也是如此。你的问题应该有足够的信息,所以我们不需要问很多问题来寻求澄清