从另一个数据帧中的列中发现信息



我有两个数据帧,它们是df_First:

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:

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_Second(。它必须包含基于车牌末端的汽车模型。

所需的输出如下所示:

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相同的行数。 非常感谢您的帮助和关注。

要解决的主要问题是第一个数据帧包含需要删除的重复关系。有几种方法可以实现结果,包括mergejoinmap。这是join方法,

map_unique = df_First.groupby('Car Plate End')['Car Model'].first()
df_Second.join(map_unique, on='Car Plate End')

最新更新