首页 > 解决方案 > 根据 output2 值从 output1 打印行

问题描述

df1

slot              Time     Location  User
56  2017-10-26 22:15:00     89        1
2   2017-10-27 00:30:00     54        1
20  2017-10-28 05:00:00     64        1
24  2017-10-29 06:00:00     2         1
91  2017-11-01 22:45:00     78        1
62  2017-11-02 15:30:00     99        1
91  2017-11-02 22:45:00     34        1
47  2017-10-26 20:15:00     465       2
1   2017-10-27 00:10:00     67        2
20  2017-10-28 05:00:00     5746      2
28  2017-10-29 07:00:00     36        2
91  2017-11-01 22:45:00     786       2
58  2017-11-02 14:30:00     477       2
95  2017-11-02 23:45:00     7322      2

df2

slot
2
91
62
58

我需要输出 df3 作为

slot              Time    Location  User
2   2017-10-27 00:30:00     54        1
91  2017-11-01 22:45:00     78        1
91  2017-11-02 22:45:00     34        1
91  2017-11-01 22:45:00     786       2
62  2017-11-02 15:30:00     99        1
58  2017-11-02 14:30:00     477       2

如果这些是 csv 文件,那么我们可以加入它们

join File1 file2 > file3

但是我们怎样才能对 Jupyter notebook 中的输出做同样的事情呢?

标签: pythonpandas

解决方案


尝试isin

df1[df1.slot.isin(df2.slot)]

输出:

    slot               Time     Location    User
1   2   2017-10-27 00:30:00     54          1
4   91  2017-11-01 22:45:00     78          1
5   62  2017-11-02 15:30:00     99          1
6   91  2017-11-02 22:45:00     34          1
11  91  2017-11-01 22:45:00     786         2
12  58  2017-11-02 14:30:00     477         2

推荐阅读