Python: Sum values in DataFrame if other values match between DataFrames

后端 未结 3 1363
孤街浪徒
孤街浪徒 2021-01-14 23:54

I have two dataframes of different length like those:

DataFrame A:

FirstName    LastName
Adam         Smith
John         Johnson

Da

相关标签:
3条回答
  • 2021-01-15 00:28

    Use boolean indexing with Series.isin for filtering and then aggregate sum:

    df = (df_b[df_b['Last'].isin(df_a['LastName'])]
               .groupby(['First','Last'], as_index=False)['Value']
               .sum())
    

    If want match both, first and last name:

    df = (df_b.merge(df_a, left_on=['First','Last'], right_on=['FirstName','LastName'])
               .groupby(['First','Last'], as_index=False)['Value']
               .sum())
    
    0 讨论(0)
  • 2021-01-15 00:40

    Use DataFrame.merge + DataFrame.groupby:

    new_df=( dfa.merge(dfb.groupby(['First','Last'],as_index=False).Value.sum() ,
                       left_on='LastName',right_on='Last',how='left')
                .drop('Last',axis=1) )
    print(new_df)
    

    to join for both columns:

    new_df=( dfa.merge(dfb.groupby(['First','Last'],as_index=False).Value.sum() ,
                  left_on=['FirstName','LastName'],right_on=['First','Last'],how='left')
                .drop(['First','Last'],axis=1) )
    print(new_df)
    

    Output:

      FirstName LastName  Value
    0      Adam    Smith    5.7
    1      John  Johnson    2.5
    
    0 讨论(0)
  • 2021-01-15 00:43
    df_b_a = (pd.merge(df_b, df_a, left_on=['FirstName', 'LastName'], right_on=['First', 'Last'], how='left')
                    .groupby(by=['First', 'Last'], as_index=False)['Value'].sum())
    
    print(df_b_a)
    
        First   Last    Value
    0   Adam    Smith   5.7
    1   John    Johnson     2.5
    
    0 讨论(0)
提交回复
热议问题