首页 > 解决方案 > 在 pandas 中选择一个 df1 而不是 df2 中的行

问题描述

我有一个 df ,如下所示。

df1:

ID     Age_days    N_30     N_31_90     N_91_180      
1      201         60       15          30            
2      20          0        15          5             
3      800         0        0           10            
4      100         0        0           0             
5      600         0        6           5             
6      800         0        0           15            
7      500         10       10          30 

df2:

 ID     Age_days    N_30     N_31_90     N_91_180      
    1      201         60       15          30            
    2      20          0        15          5
    4      100         0        0           0
    6      800         0        0           15

其中 ID 是 df1 和 df2 的主键。

我想从中选择 df1 中而不是 df2 中的行。

预期输出:

ID     Age_days    N_30     N_31_90     N_91_180             
3      800         0        0           10                        
5      600         0        6           5                        
7      500         10       10          30 

我尝试了以下代码:

df3 = df1[~df1.isin(df2)].dropna()

我在下面提到了松弛问题。 pandas 获取不在其他数据框中的行

common = df1.merge(df2,on=['col1','col2'])
print(common)
df1[(~df1.col1.isin(common.col1))&(~df1.col2.isin(common.col2))]

我的疑问是 col1 和 col2 在我的情况下应该是什么。

标签: python-3.xpandasdataframe

解决方案


推荐阅读