熊猫根据df1中另一列中的字符串删除df2中的行

时间:2020-05-11 16:46:01

标签: python python-3.x pandas

我有2个pandas数据帧,其中idfirst_seenlast_seen是字符串,days_since_fire是整数。我想从df1中的id等于'2020-05-02'的df2中删除last_seen

df1
    id  first_seen  last_seen   days_since_fire
0   001 2020-05-01  2020-05-01  0
1   002 2020-05-01  2020-05-01  0
2   003 2020-05-02  2020-05-02  0

df2
    id
0   001
1   003
2   002
3   004

所需结果:

df2
    id
0   001
2   002
3   004

1 个答案:

答案 0 :(得分:1)

以您的情况

df2=df2.loc[~df2.id.isin(df1.loc[df1.last_seen=='2020-05-02','id'])].copy()
df2
Out[393]: 
   id
0   1
2   2
3   4