我有两个看起来像这样的表:
data = [['tom', [3,5]], ['nick', [3,8]], ['juli', [3]]]
dfA = pd.DataFrame(data, columns = ['Name', 'job_id'])
data1 = [['coder', 3], ['cook', 5], ['cop', 8]]
df_B = pd.DataFrame(data1, columns = ['job', 'job_id'])
我想在第一个表中添加一列,使其看起来像这样:
data_comb = [['tom', ['coder','cook']], ['nick', ['coder','cop']], ['juli', ['coder']]]
df_comb = pd.DataFrame(data_comb, columns = ['Name', 'jobs_done'])
由于该列中的列表,我收到了无法散列的列表错误。指出如何解决此问题的指针。
答案 0 :(得分:0)
您可以使用字典来映射列表:
lookup = {key : value for value, key in data1}
dfA['job_id'] = dfA.job_id.apply(lambda x : [lookup[v] for v in x])
print(dfA)
输出
Name job_id
0 tom [coder, cook]
1 nick [coder, cop]
2 juli [coder]
答案 1 :(得分:0)
mapper = df_B.set_index('job_id').to_dict()['job']
dfA['job_id'] = dfA['job_id'].apply(lambda lst: [mapper.get(x) for x in lst])
输出:
>>>dfA
Name job_id
0 tom ['coder', 'cook']
1 nick ['coder', 'cop']
2 juli ['coder']