PANDAS -使用Map并忽略大小写

wfveoks0  于 2022-12-16  发布在  其他
关注(0)|答案(3)|浏览(174)

我有一个 Dataframe :

df1 = pd.DataFrame({'Item': ["jetties","Jetty", "Joint use"], 'Team': ["Team_1", "Team_2", "Team_1"]})
df2 = pd.DataFrame({'Item': ["Jetty","Jetties","Joint use"]})

我正在应用map返回df2中的Team值:

df2['Team'] = df2['Item'].map(df1.set_index('Item')['Team'])

但是,结果是因为大小写敏感性而给我Nan for Jetties,有没有办法忽略大小写敏感性?

rdlzhqv9

rdlzhqv91#

您可以将密钥转换为已定义的大小写:

df2['Team'] = (df2['Item'].str.lower()
               .map(df1.assign(Item=df1['Item'].str.lower())
                       .set_index('Item')['Team'])
              )

输出:

Item    Team
0      Jetty  Team_2
1    Jetties  Team_1
2  Joint use  Team_1
niknxzdl

niknxzdl2#

df = pd.merge(df1, df2, left_on=df1['Item'].str.lower(), right_on=df2['Item'].str.lower(), how='left').drop(['key_0', 'Item_x'], axis=1).rename(columns={'Item_y': 'Item'})
df = df[['Item', 'Team']]
df

大多数时候,我更喜欢连接数据而不是使用Map

wkyowqbh

wkyowqbh3#

扩展mozway的答案,可能最好按如下方式调用assign函数,这将便于以后在必要时将逻辑重构为函数。

df2["Team"] = ( 
    df2["Item"]
    .str.lower()
    .map(df1.assign(**{"Item": df1["Item"].str.lower()}).set_index("Item")["Team"])
)

例如

import pandas as pd

df1 = pd.DataFrame(
    {"Item": ["jetties", "Jetty", "Joint use"], "Team": ["Team_1", "Team_2", "Team_1"]}
)
print("df1")
print(df1)

df2 = pd.DataFrame({"Item": ["Jetty", "Jetties", "Joint use"]})
print("df2")
print(df2)

def map_series(s, df, key_col, value_col, ignore_case):
    if ignore_case:
        result = s.str.lower().map(
            df.assign(**{key_col: df[key_col].str.lower()}).set_index(key_col)[
                value_col
            ]
        )
    else:
        result = s.map(df.set_index(key_col)[value_col])
    return result

df2["Team_include_case"] = map_series(
    df2["Item"], df1, "Item", "Team", ignore_case=False
)
df2["Team_ignore_case"] = map_series(df2["Item"], df1, "Item", "Team", ignore_case=True)
print("after mapping")
print(df2)

样品输出

df1
        Item    Team
0    jetties  Team_1
1      Jetty  Team_2
2  Joint use  Team_1
df2
        Item
0      Jetty
1    Jetties
2  Joint use
after mapping
        Item Team_include_case Team_ignore_case
0      Jetty            Team_2           Team_2
1    Jetties               NaN           Team_1
2  Joint use            Team_1           Team_1

相关问题