Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.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 删除具有2个条件的数据帧行_Python_Dataframe - Fatal编程技术网

Python 删除具有2个条件的数据帧行

Python 删除具有2个条件的数据帧行,python,dataframe,Python,Dataframe,我有一个数据帧df1 269 270 271 346 0 1 153.00 2.14 1 1 1 153.21 3.89 2 2 1 153.90 2.02 1 3 1 154.18 3.02 1 4 1 154.47 2.30 1 5 1 154.66 2.73 1 6 1 155.35 2.82 1 7 1 155.70

我有一个数据帧
df1

    269     270    271  346
0     1  153.00   2.14    1
1     1  153.21   3.89    2
2     1  153.90   2.02    1
3     1  154.18   3.02    1
4     1  154.47   2.30    1
5     1  154.66   2.73    1
6     1  155.35   2.82    1
7     1  155.70   2.32    1
8     1  220.00  15.50    1
9     0  152.64   1.44    1
10    0  152.04   2.20    1
11    0  150.48   1.59    1
12    0  149.88   1.73    1
13    0  129.00   0.01    1
以及第二数据帧df2

    269      270    271  346
0     0   149.88    2.0    1
我希望删除索引
12
中的行,因为它们在列
['269']
&
['270']

中具有相同的数字。请尝试以下操作:

import pandas as pd

data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
df1 = pd.DataFrame.from_dict(data)
print(df1)

data1 = {'col_1': [3, 2], 'col_2': ['a', 'b']}
df2 = pd.DataFrame.from_dict(data1)
print(df2)


print(df1.loc[~((df1["col_1"].isin(df2['col_1']))&(df1["col_2"].isin(df2['col_2']))),:])
DF1:

DF2:

输出:

   col_1 col_2
2      1     c
3      0     d
在代码使用中:

df1.loc[~((df1["269"].isin(df2['269']))&(df1["270"].isin(df2['270']))),:]

解决此问题的一种方法是对要比较数据帧的列执行
内部
联接。然后执行
loc
查找具有
isin
的类似行,然后删除这些索引

在代码中

    m = pd.merge(df1[['269', '270']], df2[['269', '270']], how='inner', on=['269', '270'])

    df1.drop(df1.loc[((df1['269'].isin(m['269'])) & (df1['270'].isin(m['270'])))].index)

#Output
    269 270 271 346
0   1   153.00  2.14    1
1   1   153.21  3.89    2
2   1   153.90  2.02    1
3   1   154.18  3.02    1
4   1   154.47  2.30    1
5   1   154.66  2.73    1
6   1   155.35  2.82    1
7   1   155.70  2.32    1
8   1   220.00  15.50   1
9   0   152.64  1.44    1
10  0   152.04  2.20    1
11  0   150.48  1.59    1
13  0   129.00  0.01    1
df1.loc[~((df1["269"].isin(df2['269']))&(df1["270"].isin(df2['270']))),:]
    m = pd.merge(df1[['269', '270']], df2[['269', '270']], how='inner', on=['269', '270'])

    df1.drop(df1.loc[((df1['269'].isin(m['269'])) & (df1['270'].isin(m['270'])))].index)

#Output
    269 270 271 346
0   1   153.00  2.14    1
1   1   153.21  3.89    2
2   1   153.90  2.02    1
3   1   154.18  3.02    1
4   1   154.47  2.30    1
5   1   154.66  2.73    1
6   1   155.35  2.82    1
7   1   155.70  2.32    1
8   1   220.00  15.50   1
9   0   152.64  1.44    1
10  0   152.04  2.20    1
11  0   150.48  1.59    1
13  0   129.00  0.01    1