我想根据另一个数据框中的值从一个数据框中删除行。以下代码适用于某些数据框,但不适用于其他数据框。
df_filter_apply: dataframe from which rows have to be removed
df_filter_input: dataframe that contains input for the filter operation
如果df_filter_input有一行 col1 = 1,col2 = 2,col3 = 1,从df_applyfilter数据框中删除该行。
filtered_df=pd.merge(df_filter_apply,df_filter_input, how='outer', indicator=True,on=[col1","col2","col3"])
rows_in_df1_not_in_df2 = filtered_df[filtered_df['_merge']=='left_only'][df_filter_apply.columns]
return rows_in_df1_not_in_df2
上面的代码适用于某些数据帧,但对于其他数据帧则适用
KeyError: "Index(['index', 'Unnamed: 0'], dtype='object') not in index"
两个数据框的头部
df_filter_apply
Unnamed: 0 col1 col2 col3 end sentence sentence_Id start text_id Y_predicted modified_prediction
df_filter_input
Unnamed: 0 ItemId FullSliderMean FullSliderDev FullSliderN GoodSliderMean GoodSliderDev GoodSliderN Col1 col2 col3