我正在尝试完成 EDA 演练,并通过使用循环处理带有 json 数据的列来转换/提取数据框中的 json 数据。为此,我为要循环访问的循环设置一个列表,然后设置 for 循环以加载 json 数据并为每一列的每一行提取名称字段。
处理列表中的第一列后,它会抛出“JSON 对象必须是 str、bytes 或 bytearray,而不是‘list’”错误。
我已经尝试通过添加和删除列来修改列表以查看失败的地方,并且它始终适用于第一列,但在之后放弃。对于一列列表也是如此。
我认为问题是正在传递的“json.loads(data)”不知何故仍然指向最后一个循环的结果(因为来自最后一个循环的 json 被转换/提取到列表中)。但我不确定是否是这种情况,如果是,如何解决。
代码如下:
json_fields = ['genres', 'production_countries', 'spoken_languages']
for field in json_fields:
print(field)
movies_df[field] = movies_df[field].apply(lambda data:[row['name'] for row in json.loads(data)])
这是回溯:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-cc13bd0423f3> in <module>()
3 for field in json_fields:
4 print(field)
----> 5 movies_df[field] = movies_df[field].apply(lambda data:[row['name'] for row in json.loads(data)])
~/anaconda3/lib/python3.6/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
2549 else:
2550 values = self.asobject
-> 2551 mapped = lib.map_infer(values, f, convert=convert_dtype)
2552
2553 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()
<ipython-input-15-cc13bd0423f3> in <lambda>(data)
3 for field in json_fields:
4 print(field)
----> 5 movies_df[field] = movies_df[field].apply(lambda data:[row['name'] for row in json.loads(data)])
~/anaconda3/lib/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
346 if not isinstance(s, (bytes, bytearray)):
347 raise TypeError('the JSON object must be str, bytes or bytearray, '
--> 348 'not {!r}'.format(s.__class__.__name__))
349 s = s.decode(detect_encoding(s), 'surrogatepass')
350
TypeError: the JSON object must be str, bytes or bytearray, not 'list'
这是结果表的链接: https://imgur.com/a/yHDdFM8
Genres 是有效的栏目,另外两个是无效的栏目示例
编辑:这是我正在使用的表格的来源:https://www.kaggle.com/tmdb/tmdb-movie-metadata/
请您参考如下方法:
所以我认为“genre”列是一个表示 JSON 的字符串,而“production_countries”和“spoken_languages”列是包含 JSON 字符串表示形式或已经解析为 python 字典的列表。
尝试将循环更改为
for field in json_fields:
print(field)
print(type(movies_df[field][0]))
print(type(movies_df[field][0][0]))
根据输出有几个解决方案
1。 “production_countries”和“spoken_languages”是字符串列表
如果上面的循环给你类似的东西
production_countries
<class 'list'>
<class 'str'>
然后“production_countries”中的每一行都是一个列表,列表中的每个元素都是一个字符串,应该能够使用以下代码将其解析为 JSON。
for field in json_fields:
if field == 'genres':
movies_df[field] = movies_df[field].apply(lambda data: [row['name'] for row in json.loads(data)])
elif field == 'production_countries':
movies_df[field] = movies_df[field].apply(lambda data: [json.loads(row)['name'] for row in data])
2。 "production_countries"和 "spoken_languages"是 python 字典列表
如果第一个循环给你类似的东西
production_countries
<class 'list'>
<class 'dict'>
然后“production_countries”上的每一行都是一个列表,列表中的每个元素都是一个字典。然后下面应该工作
for field in json_fields:
if field == 'genres':
movies_df[field] = movies_df[field].apply(lambda data: [row['name'] for row in json.loads(data)])
elif field == 'production_countries':
movies_df[field] = movies_df[field].apply(lambda data: [row['name'] for row in data])
总结
如果上述方法不起作用,则列可能由其他数据结构组成。如果上述确实有效,最好更改数据加载到 Pandas 数据框中的方式,而不是使用上述解决方案。
