1

将dataframe两列转为字典

将dataframe两列转为字典

import pandas as pd
a = ['Mary','Sellina','Zaca']
b = [26,28,27]
data = {'name':a,'age':b}
df = pd.DataFrame(data)

将两列转为字典
name_age_dict = df.set_index(['name'])['age'].to_dict()

结果显示:
{'Mary': 26, 'Sellina': 28, 'Zaca': 27}

posted @ 2023-08-10 14:47  Bonne_chance  阅读(98)  评论(0编辑  收藏  举报
1