python - Pandas map lambda 函数的不区分大小写的字典

标签 python python-2.7 pandas lambda

下面,我在函数中使用 lambda x: 将值mappandas 列(如果它们出现在字典中)基准

在示例中,符号 “GOOG” 被映射为 “Google” 到列 “full_name”。

我的问题是:如何对字典进行不区分大小写的检查?因此,例如,"Aapl" 变成了 "Apple",即使 "AAPL" 在字典中也是如此。

import pandas as pd
import re

df = pd.read_csv(file.csv, delimiter=",")
df = pd.DataFrame(["LONG GOOG VON", "Long Aapl X4 VON"], columns=["symbol"])

benchmarks = {"GOOG": "Google", "AAPL": "Apple"}
match = re.compile(r"\s(\S+)\s")

def f(value):
    f1 = lambda x: benchmarks[match.findall(x)[0]] if match.findall(x)[0] in benchmarks else ""
    stuff = f1(value)
    #stuff done here is omitted
    return stuff

df["full_name"] = df["symbol"].map(lambda x:f(x))

最佳答案

在编译匹配的时候使用re.IGNORECASE,然后将匹配结果转为大写,供字典使用。

import re
a = ["LONG GOOG VON", "Long Aapl X4 VON", 'no matches here']
match = re.compile(r"\s(\S+)\s", re.IGNORECASE)
benchmarks = {"GOOG": "Google", "AAPL": "Apple"}
for element in a:
    s = match.search(element)
    if s:
        print(benchmarks.get(s.group(1).upper(), ''))

结果:

Google
Apple

关于python - Pandas map lambda 函数的不区分大小写的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31126361/

相关文章:

python - 有效地计算组合和排列

python - 计算每行和每个索引的周年日期

python - Clustermap Seaborn 屏蔽矩阵的上对角线,固定每个 "cell"的大小并删除连接每个单元的线

python - BeautifulSoup 不能使用 if is None then continue 语句来避免 'NoneType' object is not subscriptable Type Error

Python 方法查找,静态与实例

python - 无法在 python 2.7 中迭代元组

python - 如何让脚本在迭代中等待,直到重新建立 Internet 连接?

python - DataFrame 系列和 Pandas 面板之间的区别

python - dataframe.groupby 中排序的频率和百分比

python - 如何找到基于权重的定制平均值,包括处理 pandas 中的 nan 值?