分割键值对字符串

时间:2019-06-20 09:13:37

标签: python

我有流派列,其中包含字符串键:值示例

"[{'id': 53, 'name': 'Thriller'}, {'id': 18, 'name': 'Drama'}]"

我只需要列的名称值 注意:它不是字典,而是基于字符串的

list_of_genres = list(train['genres'].apply(lambda x :[i['name'] for i in x] if x != {} else []).values)

当我使用此代码时,它将引发错误

  

TypeError跟踪(最近的呼叫   最后)         1个         2   ----> 3 list_of_genres = list(train ['genres']。apply(lambda x:[i ['name'] for x in i]如果x!= {} else [])。values)         4 list_of_genres         5

     

D:\ anaconda3 \ lib \ site-packages \ pandas \ core \ series.py在apply(self,   func,convert_dtype,args,** kwds)3589否则:3590
  值= self.astype(object).values   -> 3591映射= lib.map_infer(值,f,convert = convert_dtype)3592 3593如果len(被映射)和   isinstance(映射[0],系列):

     pandas._libs.lib.map_infer()中的

pandas / _libs / lib.pyx

     

在(x)中         1个         2   ----> 3 list_of_genres = list(train ['genres']。apply(lambda x:[i ['name'] for x in i]如果x!= {} else [])。values)         4 list_of_genres         5

     (.0)中的

        1个         2   ----> 3 list_of_genres = list(train ['genres']。apply(lambda x:[i ['name'] for x in i]如果x!= {} else [])。values)         4 list_of_genres         5

     

TypeError:字符串索引必须为整数

0 个答案:

没有答案