通过索引将列表分配给系列

时间:2019-07-09 23:26:36

标签: python pandas

我正在尝试从数据框内的列表中删除字符串。为此,我将使用与某些条件匹配的索引列表。我正在使用for循环和列表理解来做到这一点:

to_remove = ['International']

for i in [267, 337, 619, 632, 681, 745, 934, 1341, 1379, 2059]:
    train.loc[i, 'tags'] = [x for x in train['tags'][i] if x not in to_remove]

出现以下错误:

ValueError: Must have equal len keys and value when setting with an iterable

处理一系列包含不同长度列表的系列。例如:

Tag
['Veggie', 'Internacional']
['Veggie', 'Peruvian', 'Asian', 'Latin']
['Veggie', 'Fast food', 'Latin', 'Internacional']
['Veggie']

预期输出:

 Tag
 ['Veggie']
 ['Veggie', 'Peruvian', 'Asian', 'Latin']
 ['Veggie', 'Fast food', 'Latin']
 ['Veggie']

1 个答案:

答案 0 :(得分:0)

可以使用

的IIUC
[[y for y in x if y not in to_remove ] for x in df.Tag ]
Out[339]: 
[['Veggie'],
 ['Veggie', 'Peruvian', 'Asian', 'Latin'],
 ['Veggie', 'Fast food', 'Latin'],
 ['Veggie']]