根据另一个数据帧的条件选择行

时间:2016-05-30 20:21:56

标签: python pandas

我有两个DataFrames具有不同数量的行和列,但其中至少有一列包含一些常见信息。具体而言,StationCode始终为LocationCode

dataframe1.head()

   DistanceToPrev LineCode  SeqNum StationCode           StationName  RailTime
0               0       RD       1         A15           Shady Grove         0
1           14151       RD       2         A14             Rockville         4
2           10586       RD       3         A13             Twinbrook         3
3            5895       RD       4         A12           White Flint         3
4            7309       RD       5         A11  Grosvenor-Strathmore         3
5           11821       RD       6         A10        Medical Center         3
6            5530       RD       7         A09              Bethesda         2
7            9095       RD       8         A08    Friendship Heights         3
8            4135       RD       9         A07         Tenleytown-AU         2
9            5841       RD      10         A06          Van Ness-UDC         2

dataframe2.head()

     Car Destination DestinationCode DestinationName Group Line LocationCode                LocationName  Min
0     8    Glenmont             B11        Glenmont     1   RD          A01                Metro Center  BRD
28    8    Glenmont             B11        Glenmont     1   RD          B01        Gallery Pl-Chinatown  ARR
35    6    Glenmont             B11        Glenmont     1   RD          A14                   Rockville    1
45    8    Glenmont             B11        Glenmont     1   RD          B02            Judiciary Square    2
62    6    Glenmont             B11        Glenmont     1   RD          B07                      Takoma    3
80    6    Glenmont             B11        Glenmont     1   RD          A13                   Twinbrook    4
82    8    Glenmont             B11        Glenmont     1   RD          B03               Union Station    4
95    6    Glenmont             B11        Glenmont     1   RD          B08               Silver Spring    5
114   8    Glenmont             B11        Glenmont     1   RD          B35              NoMa-Gallaudet    6
129   6    Glenmont             B11        Glenmont     1   RD          A12                 White Flint    7
143   8    Glenmont             B11        Glenmont     1   RD          B04  Rhode Island Ave-Brentwood    8

我想只获取dataframe2Min列的值RailTimedataframe1相同的行与StationCode匹配的LocationCode

例如,dataframe2中标记为80的行有LocationCode A13和Min 4.在dataframe1 StationCode中A13有RailTime 4,因此该行应该来自dataframe2 ex

相反,dataframe2中标记为35的行的LocationCode A14和Min值为1,小于RailTime来自dataframe1的值{ {1}},所以它应该 in cluded。

1 个答案:

答案 0 :(得分:2)

简单的解决方案是:

df2 = df2.merge(df1[['StationCode', 'RailTime']], left_on='LocationCode', right_on='StationCode')
df2 = df2[df2.Min<df2.RailTime]