【问题标题】:Combine two dataframes with two common columns - python [duplicate]将两个数据框与两个公共列结合起来 - python [重复]
【发布时间】:2020-05-19 15:33:31
【问题描述】:

我有两个数据框,df1

        JobId   WorkOrderNo TradeName       TradeMasterId   TechnicianId    Rating
    0   10112   samsung10112    Plumbing        1074              NaN          NaN
    1   10112   samsung10112    Plumbing        1074              NaN          NaN
    2   10112   samsung10112    Plumbing        1074              1332.0    2.962963
   295  10161   samsung10161    Carpenter       1090                NaN       NaN
   296  10161   samsung10161    Carpenter       1090              1337.0    3.724138
   297  10161   samsung10161    Carpenter       1090              1296.0    3.526316
   298  10161   samsung10161    Carpenter       1090              1296.0    3.526316

df2,

    TechnicianId    UserName    TradeMasterId   LateNight   Weekends    WeekDays
0   1296            ramzee rr   1090                True    False        True
1   1325            Arun Guna   1089                True    True         True
2   1326          Rajesh Thiru  1074                False   False        True
4   1336              RR RR     1068                False   False        True 

现在我想使用这两个数据帧中的两个公共列合并这两个数据帧。 我希望最终的数据集看起来像,

            JobId   WorkOrderNo TradeName       TradeMasterId   TechnicianId         Rating        LateNight   Weekends   Weekdays

        0   10112   samsung10112    Plumbing        1074              NaN          NaN            False  False       True
        1   10112   samsung10112    Plumbing        1074              NaN          NaN            False  False       True 
        2   10112   samsung10112    Plumbing        1074              1332.0    2.962963          False  False       True
       295  10161   samsung10161    Carpenter       1090                NaN       NaN             True   False       True
       296  10161   samsung10161    Carpenter       1090              1337.0    3.724138          True   
False        True         
       297  10161   samsung10161    Carpenter       1090              1296.0    3.526316          True   
False        True
       298  10161   samsung10161    Carpenter       1090              1296.0    3.526316          True   
False        True

谁能帮帮我?

【问题讨论】:

  • df1.merge(df2) 如果两个数据框中只有两列匹配,则不需要使用on 参数。如果不是,那么您将加入两个数据框中名称相同的列,然后使用 df1.merge(df2, on=['col1','col2'])。否则,您可以在合并中使用right_onleft_on 参数。
  • 但是两个数据框的行数不同。这行得通吗?
  • @flwaedwriter 是的,这正是 merge 所做的。

标签: python python-3.x pandas dataframe pandas-groupby


【解决方案1】:

试试这个:

new_df = pd.merge(df1, df2, on=["TradeMasterId", "TechnicianId"])

【讨论】:

    【解决方案2】:

    您可能想要使用左连接。取决于您可以使用的密钥。

    mergeddf = df.set_index("TechnicianId").join(df2.set_index("TechnicianId"))
    

    查看pandas.Dataframe.join 文档了解更多信息。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-10-09
      • 2020-11-02
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 2021-10-22
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      相关资源
      最近更新 更多