我有一列,其值存储为列表。该列如下所示:
[{'key': 'firebase_screen_class', 'value': 'MainViewController'},
{'key': 'firebase_event_origin', 'value': 'auto'},
{'key': 'firebase_screen_id', 'value': '81265142482196321'},
{'key': 'engagement_time_msec', 'value': '3374'}]
我想将其转换为如下所示的列:
{'firebase_screen_class': 'MainViewController',
'firebase_event_origin': 'auto',
'firebase_screen_id': '81265142482196321',
'engagement_time_msec': '3374'}
答案 0 :(得分:4)
使用字典理解:
{d['key']:d['value'] for d in col}
其中col
是您的原始列。
答案 1 :(得分:1)
您可以简单地使用for循环来实现它:
l1 = [{'key': 'firebase_screen_class', 'value': 'MainViewController'},
{'key': 'firebase_event_origin', 'value': 'auto'},
{'key': 'firebase_screen_id', 'value': '81265142482196321'},
{'key': 'engagement_time_msec', 'value': '3374'}]
final_dict = {}
for d1 in l1:
final_dict[d1['key']] = d1['value']
print final_dict
答案 2 :(得分:1)
list = [{'key': 'firebase_screen_class', 'value': 'MainViewController'},
{'key': 'firebase_event_origin', 'value': 'auto'},
{'key': 'firebase_screen_id', 'value': '81265142482196321'},
{'key': 'engagement_time_msec', 'value': '3374'}]
result = dict()
for d in list:
result[d['key']] = d['value']
答案 3 :(得分:0)
def dd(arr):
di = {}
for j in arr:
di[j['key']] = j['value']
return di
df['new_cil'] = df['col'].map(dd)