请帮我一个JSONDecodeError

时间:2019-05-05 20:51:01

标签: python jupyter-notebook

代码未完全执行。我得到一条消息,即某种程度上存在JSON转换错误

原始代码:

# Unpack the JSON data in these fields:
json_fields = ['genres', 'production_countries', 'spoken_languages']
for field in json_fields:
    movies_df[field] = movies_df[field].apply(lambda data: [row['name'] for row in json.loads(data)])

我已经尝试了几件事,但是没有任何效果。 该示例来自here。 也许您可以帮助我

这是错误消息:

    ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-8ee46b864b3c> in <module>
      1 json_fields = ['genres', 'production_countries', 'spoken_languages']
      2 for field in json_fields:
----> 3     movies_df[field] = movies_df[field].apply(lambda data: [row['name'] for row in json.loads(data)])

/anaconda3/envs/Masterprojekt/lib/python3.7/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   3589             else:
   3590                 values = self.astype(object).values
-> 3591                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   3592 
   3593         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

<ipython-input-9-8ee46b864b3c> in <lambda>(data)
      1 json_fields = ['genres', 'production_countries', 'spoken_languages']
      2 for field in json_fields:
----> 3     movies_df[field] = movies_df[field].apply(lambda data: [row['name'] for row in json.loads(data)])

/anaconda3/envs/Masterprojekt/lib/python3.7/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    339     else:
    340         if not isinstance(s, (bytes, bytearray)):
--> 341             raise TypeError(f'the JSON object must be str, bytes or bytearray, '
    342                             f'not {s.__class__.__name__}')
    343         s = s.decode(detect_encoding(s), 'surrogatepass')
     

TypeError :JSON对象必须是str,字节或字节数组,而不是列表

0 个答案:

没有答案