Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.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_Pandas_Dataframe - Fatal编程技术网

Python 数据帧列的不区分大小写匹配

Python 数据帧列的不区分大小写匹配,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个数据帧和一个行选择函数 import pandas as pd df = pd.DataFrame({'A':[1,2,3],'B':[5,6,7]}) def select_f(row): return row['a'] 问题是我不想更改列名(保持大写)并运行以下函数 for _, row in df.iterrows: if select_f(row) >2: print row['B'] 您可以对正则表达式使用df.filter: In [

我有一个数据帧和一个行选择函数

import pandas as pd
df = pd.DataFrame({'A':[1,2,3],'B':[5,6,7]})
def select_f(row):
    return row['a']
问题是我不想更改列名(保持大写)并运行以下函数

for _, row in df.iterrows:
    if select_f(row) >2:
        print row['B']

您可以对正则表达式使用
df.filter

In [246]: df.filter(regex=re.compile('^a$', re.I))
Out[246]: 
   A
0  1
1  2
2  3
出于您的目的,您可以使用:

def select_f(row):
    return row.filter(regex=re.compile('^a$', re.I)).iloc[0]

谢谢你的快速回答,虽然不是很有效,但我想返回最后一行。您的代码仍然打印所有row@KEXINWANG您必须将该行传递给函数。这项工作对我来说是:def select_f(row):return row.filter(regex=re.compile(“^a$”,re.I))。值[0]谢谢您的帮助