将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}