让我们假设我们跟随DF挥手:
+-----------+-------------+
| Indicator | Value |
+-----------+-------------+
| gnipc | 540.00 |
| survinf | 1157321.00 |
| Pop | 38928341.00 |
| u5mort | 62.28 |
+-----------+-------------+我想创建一本字典:
{'gnipc':540.00,
'survinf':1157321.00,
'Pop':38928341.00,
'u5mort':62.28}当我在做的时候
dict = df.to_dict('records')我正在获取字典列表:
[{'Indicator': 'gnipc', 'Value': 540.0},
{'Indicator': 'survinf', 'Value': 1157321.0},
{'Indicator': 'Pop', 'Value': 38928341.0},
{'Indicator': 'u5mort', 'Value': 62.28}]而且我必须做一些额外的步骤来获得所需的字典。如何获取此信息:
{'gnipc':540.00,
'survinf':1157321.00,
'Pop':38928341.00,
'u5mort':62.28}以最有效的方式?
发布于 2020-09-03 23:14:28
这样就可以了。
result = {row["Indicator"]: row["Value"] for row in df.to_dict('records')}https://stackoverflow.com/questions/63726427
复制相似问题