python - pandas 数据框列的不区分大小写匹配

标签 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 [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]

关于python - pandas 数据框列的不区分大小写匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45860916/

相关文章:

python - 如何使用 FastAPI 上传文件和 JSON 数据?

针对 Azure 服务管理 API 的 Python HTTPS 在 Windows 上失败

python - Pandas :在两个数据帧上具有函数的矢量化

python - 将历史盘中数据提取到每周范围内的多个调用中

python - 为什么 pandas 多索引数据帧切片看起来不一致?

python - 将数据帧从 Python 写入 MySQL

python - 当想要查找得分最高的 `start` 标记时,torch.argmax() 中出现 TypeError

python - 如何使用此数据将元素附加到 DataFrame 中?

python - Pandas 计数正/负/中性值

python - 根据先前的列集创建多个新列(更有效)