2 回答

TA貢獻1796條經驗 獲得超4個贊
解決此問題的一種方法是對inner要比較數據框的列進行連接。然后執行 aloc以找到類似的行,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

TA貢獻1818條經驗 獲得超8個贊
試試這個:
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:
col_1 col_2
0 3 a
1 2 b
2 1 c
3 0 d
DF2:
col_1 col_2
0 3 a
1 2 b
輸出:
col_1 col_2
2 1 c
3 0 d
在您的代碼中使用:
df1.loc[~((df1["269"].isin(df2['269']))&(df1["270"].isin(df2['270']))),:]
添加回答
舉報