Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sum Two columns If Other Columns Are Equal

I’m trying to use pandas to add columns Amount and Amount2 only if Account is in Account2.

input

 Account      Amount    Account2     Amount2
0   0001957331   409.50  0404618555    26.31
1   0404618555  1535.40  0490812351     5.31
2   0490812351   338.12  0521656066   787.94
3   0696978386    13.11  0696978386   700.55

expected output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

0001957331   409.50
0404618555  1561.71
0490812351   343.43
0696978386   713.66

my code:

    df = pd.DataFrame(allc_acct_amt, columns=["Account", "Amount", "Account2", "Amount2"])
    print(df)
    dff = df.groupby(["Account","Account2"])[['Amount', 'Amount2']].sum().reset_index()
    print(dff)

>Solution :

Use merge:

out = pd.merge(df[['Account', 'Amount']], df[['Account2', 'Amount2']], 
               left_on='Account', right_on='Account2', how='left')

out = out.assign(Amount=out[['Amount', 'Amount2']].sum(1)) \
         .drop(columns={'Account2', 'Amount2'})

Output:

>>> out
      Account   Amount
0  0001957331   409.50
1  0404618555  1561.71
2  0490812351   343.43
3  0696978386   713.66

Setup to be reproducible:

data = {'Account': ['0001957331', '0404618555', '0490812351', '0696978386'],
        'Amount': [409.5, 1535.4, 338.12, 13.11],
        'Account2': ['0404618555', '0490812351', '0521656066', '0696978386'],
        'Amount2': [26.31, 5.31, 787.94, 700.55]}
df = pd.DataFrame(data)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading