总结两个数据帧pandas python

时间:2017-10-10 15:06:46

标签: python dataframe

我有两个数据帧

         colA    colB   amount1    amount2     key  
maindf = xx      yy     12         1           55_ab
         qs      rr     13         2           14_rs

        colA    colB   amount1     amount2     key           
df =    dd      tt     10          5           55_ab

我想根据字段键将数据帧df的字段amount1和数量2的值添加到maindf以获取以下内容

         colA    colB   amount1    amount2     key  
maindf = xx      yy     22         6           55_ab
         qs      rr     13         2           14_rs

3 个答案:

答案 0 :(得分:0)

如何使用pandas - 合并df而不是创建新的列/列,如下所述(只需两列) join - left = maindf,right = df,how =' left',left_on =' key',right_on =' key'

import pandas as pd
df = pd.DataFrame({'$a':[1,2], '$b': [10,20]}) # creating fake df
df2 = pd.DataFrame({'$a':[1,2], '$b': [10,20]}

dfMerge = pd.merge(left=df, right=df2, how='left', left_on='$a', right_on='$a')
dfMerge["SUM"] = dfMergeTemp["$b_x"] + dfMergeTemp["$b_y"]

# print(dfMergeTemp)

在您的情况下(也许重命名您的列不要混淆正在发生的事情)

mergedDf = pd.merge(left = maindf, right = df, how = 'left', left_on='key', right_on = 'key')

答案 1 :(得分:0)

这个怎么样?

# make main df
main = pd.DataFrame({'ColA' : ['xx', 'qs'],
                     'ColB' : ['yy', 'rr'],
                     'amount1' : [12, 13],
                     'amount2' : [1, 2],
                     'key' : ['55_ab', '14_rs']})

# make other df
df = pd.DataFrame({'ColA' : ['dd'],
                   'ColB' : ['tt'],
                   'amount1' : [10],
                   'amount2' : [5],
                   'key' : ['55_ab']})

# merge dataframes
df1 = main.merge(df,
                 how = 'left',
                 left_on = 'key',
                 right_on = 'key').fillna(0)

# sum amount1
df1.insert(df1.shape[1],
           'amount1',
           df1.amount1_x + df1.amount1_y)

# sum amount2
df1.insert(df1.shape[1],
           'amount2',
           df1.amount2_x + df1.amount2_y)

# drop unneeded columns
df1.drop(['amount1_x',
          'amount2_x',
          'ColA_y',
          'ColB_y',
          'amount1_y',
          'amount2_y'],
         axis = 1,
         inplace = True)

df

答案 2 :(得分:0)

Pôle        Libellé entité          PNB N-1         PNB N initial       Key
IBFS        PJSC ROSBANK            -200            0                   zz_t55
IBFS        PJSC ROSBANK            0               1500                bb_y44  
IBFS        PJSC ROSBANK            1000            2000                az_89   
IBFS        PJSC ROSBANK            5000            -250                rr_14

所以这就是maindf 让我们说有更多的列,我不能发布一切。 所以我想要从另一个数据帧中加总值,这些数据帧与字段“PNB N-1”和“PNB N-1”相同。和' PNB N初始'