我有一个带有 2 列的 pandas DataFrame。如何将其转换为以 id
为键、name
为值的 Python 字典?
输入
id | name
1 | hello
2 | world
要求的输出
{
"1": "hello",
"2": "world",
}
最佳答案
你可以这样做:
In [1256]: df.set_index('id').to_dict()['name']
Out[1256]: {1: 'hello', 2: 'world'}
关于python - 如何使用字符串索引字符串值将 pandas DataFrame 转换为 dict?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61922907/