pandas映射列表到新列的字典项



我有df like:

col_A
[1,2,3]
[2,3]
[1,3]

和像

这样的字典
dd = {1: "Soccer", 2: "Cricket", 3: "Hockey"}

如何创建一个新的列col_B:

col_A      col_B
[1,2,3]    ["Soccer", "Cricket", "Hockey"]
[2,3]      ["Cricket", "Hockey"]
[1,3]      ["Soccer", "Hockey"]

尝试如下:

df['sports'] = df['col_A'].map(dd)

有错误:

TypeError: unhashable type: 'list'

您可以使用if的列表推导式来过滤掉不匹配的值:

df['sports'] = df['col_A'].map(lambda x: [dd[y] for y in x if y in dd])

如果不匹配则替换为None:

df['sports'] = df['col_A'].map(lambda x: [dd.get(y, None) for y in x])

如果不匹配则返回相同的值:

df['sports'] = df['col_A'].map(lambda x: [dd.get(y, y) for y in x])

:

df['sports1'] = df['col_A'].map(lambda x: [dd[y] for y in x if y in dd])
df['sports2'] = df['col_A'].map(lambda x: [dd.get(y, None) for y in x])
df['sports3'] = df['col_A'].map(lambda x: [dd.get(y, y) for y in x])
print (df)
col_A                    sports1                          sports2  
0  [1, 2, 3, 5]  [Soccer, Cricket, Hockey]  [Soccer, Cricket, Hockey, None]   
1        [2, 3]          [Cricket, Hockey]                [Cricket, Hockey]   
2        [1, 3]           [Soccer, Hockey]                 [Soccer, Hockey]   
sports3  
0  [Soccer, Cricket, Hockey, 5]  
1             [Cricket, Hockey]  
2              [Soccer, Hockey] 
col_A=[[1,2,3],[2,3],[1,3]]
df=pd.DataFrame({'col_A':col_A})
dictLkup = {1: "Soccer", 2: "Cricket", 3: "Hockey"}
def lookupfunc(mylist):
retlist=[] 
[retlist.append(dictLkup[x]) for x in mylist]
return(retlist)
df['col_B']=df['col_A'].apply(lambda x: lookupfunc(x))
print(df.head())

相关内容

  • 没有找到相关文章

最新更新