根据另一个datafram中的条件选择行

2024-10-03 21:29:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个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

我只想获取dataframe2中的行,其Min列的值小于dataframe1中与LocationCode匹配的相同的StationCode列的值。在

例如,dataframe2中标记为80的行有LocationCodeA13和Min4。在dataframe1中,StationCodeA13有RailTime4,因此该行应该从dataframe2中包含ex。在

相反,在dataframe2中标记为35的行具有LocationCodeA14和Min值1,这小于dataframe1中A14的RailTime值,因此它应该包含在中。在


Tags: rdminheada13b11a12a14dataframe1