简体   繁体   English

将包含 DataFrame 中字典列表的字符串转换为字典列表

[英]Convert string containing list of dictionaries in DataFrame to list of dictionaries

I'm having an issue that I haven't been able to solve for two days now, despite massive amount of googling.尽管进行了大量的谷歌搜索,但我遇到了一个问题,我已经两天没能解决了。 I've been downloading data from crunchbase.com.我一直在从 crunchbase.com 下载数据。 I stored the raw data in a DataFrame.我将原始数据存储在 DataFrame 中。 However, one variable is stored as a string, which should really be a list of dictionaries.但是,一个变量存储为一个字符串,它实际上应该是一个字典列表。

Looking at a specific element of the pandas Series yields a string:查看熊猫系列的特定元素会产生一个字符串:

"[{'entity_def_id': 'category', 'permalink': 'media-and-entertainment', 'uuid': '78b58810-ad58-a623-2a80-2a0e3603a544', 'value': 'Media and Entertainment'}, {'entity_def_id': 'category', 'permalink': 'tv', 'uuid': '86d91a85-ff9d-93db-4688-3b608fee756c', 'value': 'TV'}, {'entity_def_id': 'category', 'permalink': 'tv-production', 'uuid': '47592b2e-aaaa-6aa3-d0e9-82ab5e525c2d', 'value': 'TV Production'}]"

DataFrame 中的特定列

Note that some observations in the Series in which this str of list of dicts is stored are missing (if that matters).请注意,存储此字典列表 str 的系列中的一些观察结果丢失(如果这很重要)。

I would like to create new columns in my DataFrame where the column name corresponds to the key and for each observation the corresponding value from the dict;我想在我的 DataFrame 中创建新列,其中列名对应于键,并且对于每个观察,字典中的对应值; however, I don't know how to do that since it is a string, which I can only index with integers, rather than accessing the dictionaries directly.但是,我不知道该怎么做,因为它是一个字符串,我只能用整数索引它,而不是直接访问字典。 In fact, what事实上,什么

I've tried to use json.loads, which gives me a TypeError: the JSON object must be str, bytes or bytearray, not Series.我尝试使用 json.loads,这给了我一个 TypeError:JSON 对象必须是 str、bytes 或 bytearray,而不是 Series。

I also tried ast.literal_eval(), which gives me a ValueError: malformed node or string: 0.我也试过 ast.literal_eval(),它给了我一个 ValueError: malformed node or string: 0。

Grateful for any hints and apologies if my formatting/style is not good, it's my first time posting here.如果我的格式/风格不好,感谢任何提示和道歉,这是我第一次在这里发帖。

Just use the eval() function只需使用eval()函数

import pandas as pd

s = "[{'entity_def_id': 'category', 'permalink': 'media-and-entertainment', 'uuid': '78b58810-ad58-a623-2a80-2a0e3603a544', 'value': 'Media and Entertainment'}, {'entity_def_id': 'category', 'permalink': 'tv', 'uuid': '86d91a85-ff9d-93db-4688-3b608fee756c', 'value': 'TV'}, {'entity_def_id': 'category', 'permalink': 'tv-production', 'uuid': '47592b2e-aaaa-6aa3-d0e9-82ab5e525c2d', 'value': 'TV Production'}]"

l = eval(s)

df = pd.DataFrame(l)

Out[1]: 
  entity_def_id  ...                    value
0      category  ...  Media and Entertainment
1      category  ...                       TV
2      category  ...            TV Production

[3 rows x 4 columns]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM