2016-05-30 19 views
3

DataFramesは行と列の数が異なりますが、いくつかの共通情報を含む少なくとも1つの列があります。具体的には、StationCodeは常にLocationCodeです:私はそのMin列値LocationCode一致する同じStationCodeためdataframe1未満RailTime列があるdataframe2の行のみを取得したい別のデータフレームの基準に基づいて行を選択

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 80標識はdataframe1 StationCode A13でLocationCodeMin A13及び4を有する行がdataframe2からEX cludedなければならないので、RailTime 4を有しています。一方

、行がdataframe2 35はLocationCodedataframe1 A14とからA14ためRailTime値未満であるので、それはでcludedなければならない1のMin値を有する標識されました。

答えて

1

簡単な解決策は次のようになります。

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

シンプルでありながら効果的な、ステファン。それは完全に動作します。私は投票するだろうが、まだ十分な担当者ではない。受け入れられました。 – Keith

+0

あなたが大歓迎です、うれしかったです。 – Stefan

関連する問題