Python-如果存在于另一个df列中,则从df列中删除元组

时间:2018-07-20 11:56:59

标签: python pandas tuples

我有一个函数可以从df列中删除一个字符串,如果该字符串出现在另一个df的列中:

def frameFocusCreator(frame, focusState):
    def changeState(event):
        frame.state([focusState])
    return changeState
for i in range(5):
    frame1 = ttk.Frame(frame, style="RoundedFrame", padding=10)
    journal1 = tk.Text(frame1, borderwidth=0, highlightthickness=0,  width = 40, height = 38)
    journal1.configure(relief="groove")
    journal1.configure(background="white")
    journal1.pack(fill='both', expand=True)
    journal1.bind("<FocusIn>", frameFocusCreator(frame1, "focus"))
    journal1.bind("<FocusOut>", frameFocusCreator(frame1, "!focus"))
    frame1.grid(row=0,column=i, sticky = 'nswe')

问题在于,我现在必须在一列元组上使用此功能,而该功能无法使用。有没有一种方法可以轻松地转换上述功能以适应元组?数据:

df1['col'] = df1['col'][~df1['col'].isin(df2['col'])]

如果首先将列转换为字符串,则该函数确实起作用,但是稍后在我的代码中会引发错误(我尝试在删除元组后使用.astype(tuple)命令来解决此问题,但是相同错误出现):

  

ValueError:太多值无法解包(预期2)

1 个答案:

答案 0 :(得分:1)

这将为您提供所需的输出:

df1.loc[~df1['col1'].isin(df2['col'])].reset_index(drop=True)
#                           col1
#0    (carol.clair, mark.taylor)
#1  (andrew.french, jack.martin)
#2   (ellis.taylor, sam.johnson)