如果元组中的项不在其他数据框中的列表中,则删除元组

时间:2019-06-22 12:30:24

标签: python pandas list tuples

如果我有一个元组列表,以及另一个以元组为条件的列表:

my_list=['a','e']
my_list1= ['a','b','a','c','b','d','e','f','a','h']
v=[data for data in zip(my_list1,my_list1[1::1])]
del v[1::2]

现在仅保留我使用的my_list中包含elemtns的元组

v = [tup for tup in v if any(c in my_list for c in tup)]

返回[('a', 'b'), ('a', 'c'), ('e', 'f'), ('a', 'h')] 现在我想对数据帧执行相同的操作

df15=pd.DataFrame({'names':[['a','c'],['k','f']]})
df14=pd.DataFrame({'tup':[[('a','g'), ('b','h'), ('c', 'i')],[('d', 'j'), ('e', 'k'), ('f', 'l')]]})

但是打电话

for index,row in df14.iterrows():
df15['tup']=[tup for tup in df14['tup'] if any(c in df15.names for c in tup)]

返回此错误:

  

值的长度与索引的长度不匹配

预期输出为

 df15    
            names     tup
    0     ['a','c']  [('a','g'),('c', 'i')] #not necessary to keep the tuples
    1     ['k','f']  [('e', 'k'),('f','l')]

0 个答案:

没有答案
相关问题