我有一个pandas dataframe列,其中包含如下名称列表:
Names
Roger Williams, Anne Graham
Joe Smoe, Elliot Ezekiel
Todd Roger
带有user_ids的字典:
map = {Roger Williams: 1234, Anne Graham: 4892, Joe Smoe: 898, Elliot Ezekiel: 8458, Todd Roger: 856}
我需要使用pandas .map函数将user_id映射到列表中的每个名称,如下所示:
Names user_id
Roger Williams, Anne Graham 1234, 4892
Joe Smoe, Elliot Ezekiel 898, 8458
Todd Roger 856
有人可以帮助我实现这个目标吗?我真的很难过。谢谢!
答案 0 :(得分:3)
In [124]: mapping
Out[124]:
{'Anne Graham': 4892,
'Elliot Ezekiel': 8458,
'Joe Smoe': 898,
'Roger Williams': 1234,
'Todd Roger': 856}
In [125]: df
Out[125]:
Names
0 Roger Williams, Anne Graham
1 Joe Smoe, Elliot Ezekiel
2 Todd Roger
In [126]: df.replace(mapping.keys(), list(map(str, mapping.values())), regex=True)
Out[126]:
Names
0 1234, 4892
1 898, 8458
2 856
如果你有一个字符串列表:
In [131]: df
Out[131]:
Names
0 [Roger Williams, Anne Graham]
1 [Joe Smoe, Elliot Ezekiel]
2 [Todd Roger]
In [133]: df.Names.apply(', '.join).replace(mapping.keys(), list(map(str, mapping.values())), regex=True)
Out[133]:
0 1234, 4892
1 898, 8458
2 856
Name: Names, dtype: object
答案 1 :(得分:2)
利用pd.Series
和astype(str)
df.replace(pd.Series(m).astype(str), regex=True)
Names
0 1234, 4892
1 898, 8458
2 856
设置
df = pd.DataFrame({
'Names': [
'Roger Williams, Anne Graham',
'Joe Smoe, Elliot Ezekiel',
'Todd Roger'
]
})
m = {
'Roger Williams': 1234, 'Anne Graham': 4892,
'Joe Smoe': 898, 'Elliot Ezekiel': 8458, 'Todd Roger': 856
}
答案 2 :(得分:0)
这是一种方法:
import pandas as pd
map = {'Roger Williams': 1234, 'Anne Graham': 4892, 'Joe Smoe': 898,
'Elliot Ezekiel': 8458, 'Todd Roger': 856}
df1 = pd.DataFrame.from_dict(map, orient='index')
df = df1.reset_index().rename(columns={'index': 'name', 0: 'user_id'})
答案 3 :(得分:0)
def get_values(g):
return ", ".join([map[x] for x in g])
df['Names'] = df['Names'].map(get_values)
0 1234, 4892
1 898, 8458
2 856
Name: Names, dtype: object