Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python基于另一个变量较少的数据帧从数据帧中删除行_Python_Python 3.x_Pandas_Numpy_Dataframe - Fatal编程技术网

Python基于另一个变量较少的数据帧从数据帧中删除行

Python基于另一个变量较少的数据帧从数据帧中删除行,python,python-3.x,pandas,numpy,dataframe,Python,Python 3.x,Pandas,Numpy,Dataframe,我有这样的df1: id 1 2 3 4 5 0 1 1 0 0 0 1 1 0 1 0 0 2 1 0 0 0 1 id 1 2 3 4 5 1 1 0 1 0 0 I具有以下值的df(更少的列,更少的案例): id 1 2 5 0 1 1 0 1 1 0 1 我想从df1中删除与df2中共享相同值的行,因此最终df如下所示: id 1 2 3 4 5 0 1 1 0 0 0 1 1 0

我有这样的df1:

id 1  2  3  4  5
0  1  1  0  0  0 
1  1  0  1  0  0
2  1  0  0  0  1
id 1  2  3  4  5
1  1  0  1  0  0
I具有以下值的df(更少的列,更少的案例):

id 1  2  5  
0  1  1  0
1  1  0  1
我想从df1中删除与df2中共享相同值的行,因此最终df如下所示:

id 1  2  3  4  5
0  1  1  0  0  0 
1  1  0  1  0  0
2  1  0  0  0  1
id 1  2  3  4  5
1  1  0  1  0  0
我删除了两行,因为df1和df2在其对应的列上共享相同的值


谢谢大家!

这将解决您的问题:

print (pd.merge(df1,df2, indicator=True, how='outer')
         .query('_merge=="left_only"')
         .drop('_merge', axis=1))

我希望这能帮助你找到解决办法
df2
是一个数据帧,其他两个数据帧的交集基于三个相同的列
cleared_df
是初始的
df
,交叉口除外

#Replicating the question's input
d={1:[1,1,1],2:[1,0,0],3:[0,1,0],4:[0,0,0],5:[0,0,1]}
d1={1:[1,1],2:[1,0],5:[0,1]}
df = pd.DataFrame(data=d)
df1 = pd.DataFrame(data=d1)
#Make df with the same records on 1,2,5
df2=pd.merge(df, df1, on=[1,2,5], how='inner')
#Concat the initial df with the one with the same records, then drop the duplicates
cleared_df=pd.concat([df, df2]).drop_duplicates(keep=False)

“id”是一列还是索引?在你的问题中什么是
df2