用于Pandas映射lambda函数的不区分大小写的字典

时间:2015-06-29 22:04:31

标签: python python-2.7 pandas lambda

如果它们显示在字典lambda x:中,我会在map列的函数中使用pandas benchmarks值。

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

我的问题是:如何针对字典进行不区分大小写的检查?例如,即使"Aapl"在dict中,"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))

1 个答案:

答案 0 :(得分:1)

编译匹配时使用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