在 pandas 中列出字典?

List to Dictionary in pandas?

我有一个列,其中的值存储为列表。该列看起来像:

[{'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'}

您可以简单地使用 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

使用字典理解:

{d['key']:d['value'] for d in col}

其中 col 是您的原始列。

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']
def dd(arr):
    di = {}
    for j in arr:
        di[j['key']] = j['value']
    return di

df['new_cil'] = df['col'].map(dd)