Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.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_Pandas_Dataframe - Fatal编程技术网

Python 基于在另一个数据帧中的查找删除数据帧中的行

Python 基于在另一个数据帧中的查找删除数据帧中的行,python,pandas,dataframe,Python,Pandas,Dataframe,我使用两个数据帧。我想根据另一个数据帧中的匹配删除第一个数据帧中的行 在df1中,我有两列(称为Type1和Type2)+一个标志。 我想删除flag=True和Type1&Type2与另一个df2中的组合相匹配的行 import pandas as pd import numpy as np df1 = pd.DataFrame(np.random.randint(0,10,size=(100, 2)),columns = ["Type1","Type2"]) df1["Flag"] = np

我使用两个数据帧。我想根据另一个数据帧中的匹配删除第一个数据帧中的行

在df1中,我有两列(称为Type1和Type2)+一个标志。 我想删除flag=True和Type1&Type2与另一个df2中的组合相匹配的行

import pandas as pd
import numpy as np
df1 = pd.DataFrame(np.random.randint(0,10,size=(100, 2)),columns = ["Type1","Type2"])
df1["Flag"] = np.random.randint(0,10,size=(100))>6
df1.head()

  Type1  Type2   Flag
0      8      5  False
1      1      6  False
2      9      2  False
3      0      9   True
4      2      9  False

df2 = pd.DataFrame(np.random.randint(0,10,size=(100, 2)),columns = ["Type1","Type2"])
df2.head()

  Type1  Type2
0      0      9
1      7      8
2      5      1
3      3      3
4      3      2
例如,df1中索引为3的行应删除为Flag=True,df2中存在(0,9)。

用于一个df,然后根据
df1
中的仅需值(
left_only
)和
Flag
中的
False
进行过滤,因此删除
True
中的

#on parameter omitted if only matched column are same in both df 
df3 = pd.merge(df1, df2, how='left', indicator=True)
#if multiple matched columns
#df3 = pd.merge(df1, df2, how='left', indicator=True, on = ['Type1','Type2'])
print (df3)
   Type1  Type2   Flag     _merge
0      8      5  False  left_only
1      1      6  False  left_only
2      9      2  False  left_only
3      0      9   True       both
4      2      9  False  left_only

df3 = df3.loc[(df3['_merge'] == 'left_only') & (~df3['Flag']), ['Type1','Type2']]
print (df3)
   Type1  Type2
0      8      5
1      1      6
2      9      2
4      2      9

还可以创建掩码,然后仅过滤
df1
(如果有许多列):


谢谢,太好了。我很难摆脱SQL逻辑。
m = (df3['_merge'] == 'left_only') & (~df3['Flag'])
df1 = df1[m]
print (df1)
   Type1  Type2   Flag
0      8      5  False
1      1      6  False
2      9      2  False
4      2      9  False