【发布时间】:2022-01-22 12:41:11
【问题描述】:
在数据框中有两列,我想使用第一列值作为键,另一列作为字典
假设df如下
| Variable | Value | Value Distribution | |
|---|---|---|---|
| 1 | First Color | ['Black', 'Blue', 'Green', 'Red', 'Purple'] | [0.3, 0.25, 0.2, 0.15, 0.1] |
| 5 | Second Color | ['Deep Blue', 'Teal', 'Green', 'Purple ', 'Red... | [0.5, 0.25, 0.15, 0.25, 0.25, 0.15, 0.1] |
| 6 | Third Color | ['Red', 'Orange', 'Yellow', 'Green', 'Blue', '... | [1.0, 0.0, 0.0, 0.0, 0.0, 0.0] |
所以假设我想创建一个类似的 dic
{'第一种颜色':{'Black':0.3,'Blue':0.25,'Green':0.2,'Red':0.15,'Purple':0.1}
所以我尝试了以下
dict(zip(df['Value'],df['Value Distribution']))
将第二个和第三个列值压缩到一个字典中,而不是它创建了这个字典
"['Black', 'Blue', 'Green', 'Red', 'Purple']":"[0.3, 0.25, 0.2, 0.15, 0.1]"
将列表读取为字符串
【问题讨论】:
标签: python pandas list dataframe