Python 从另一个数据帧中的列中查找信息

Python 从另一个数据帧中的列中查找信息,python,pandas,dataframe,merge,Python,Pandas,Dataframe,Merge,我有两个数据帧,首先是df_: df_First = pd.DataFrame({'Car Model': ['Fiesta 2010', 'Fiesta 2010', 'Cruze 2020', 'Fiesta 2005'], 'Car Plate End': [749, 749, 100, 200],

我有两个数据帧,首先是df_:

      df_First = pd.DataFrame({'Car Model': ['Fiesta 2010', 'Fiesta 2010', 'Cruze 2020', 'Fiesta 
                               2005'], 
                              'Car Plate End': [749, 749, 100, 200],
                              'Car Color': ['Red', 'Red', 'Blue', 'Black'],
                              'Num Door': [2,2,4,4]})
      print(df_First)


      Car Model        Car Plate End    Car Color   Num Door
     Fiesta 2010          749             Red         2
     Fiesta 2010          749             Red         2
     Cruze 2020           100             Blue        4
     Fiesta 2005          200             Black       4
第二个:

        df_Second = pd.DataFrame({'Car Plate End': [749, 749, 749, 100, 749, 100, 200, 500], 
                                  'Cost_Max': [10, 20, 30, 40, 50, 60, 70, 80],
                                  'Cost_Min': [1, 2, 3, 4, 5, 6, 7, 8]})
       print(df_Second)

      Car Plate End   Cost_Max  Cost_Min
          749           10         1
          749           20         2
          749           30         3
          100           40         4
          749           50         5
          100           60         6
          200           70         7
          500           80         8
我想创建一个新的数据帧,它是相同的 作为df_秒的行数。它必须包含基于汽车牌照末端的汽车模型

所需的输出如下:

      Car Plate End   Cost_Max  Cost_Min  Car Model
          749           10         1        Fiesta 2010
          749           20         2        Fiesta 2010
          749           30         3        Fiesta 2010
          100           40         4        Cruze 2020
          749           50         5        Fiesta 2010
          100           60         6        Cruze 2020
          200           70         7        Fiesta 2005    
          500           80         8        NaN
我尝试实现以下代码:

       df_Total = pd.merge(df_Second, df_First, on=['Car Plate End'], how='outer')
然而,我的离开并没有如预期的那样。输出为:

       Car Plate End    Cost_Max    Cost_Min    Car Model     Car Color  Num Door
            749            10          1        Fiesta 2010     Red      2.0
            749            10          1        Fiesta 2010     Red      2.0
            749            20          2        Fiesta 2010     Red      2.0
            749            20          2        Fiesta 2010     Red      2.0
            749            30          3        Fiesta 2010     Red      2.0
            749            30          3        Fiesta 2010     Red     2.0
            749            50          5        Fiesta 2010     Red     2.0
            749            50          5        Fiesta 2010     Red     2.0
            100            40          4        Cruze 2020      Blue    4.0
            100            60          6        Cruze 2020      Blue    4.0
            200            70          7        Fiesta 2005     Black   4.0
            500            80          8        NaN             NaN     NaN
我只是想知道df_Second指的是哪种车型。我不需要其他栏目。我还希望df_Total的行数与df_Second的行数相同。
非常感谢您的帮助和关注。

需要解决的主要问题是,您的第一个数据帧包含需要删除的重复关系。有几种方法可以实现该结果,包括合并、连接、映射。这里是连接方法

map_unique = df_First.groupby('Car Plate End')['Car Model'].first()

df_Second.join(map_unique, on='Car Plate End')

@JaneBorges尝试使用merge,在第一个上删除重复项后,进行左合并