0

I have 3 columns, Currency, then Column A, Column B and COlumn C.

If Currency column has value GBP, then I want to sum Column A and Column C and the output to be reflected in Column D. if value is Eur, then sum column B and Column C, and output in new column to be created D. I have tried one form or another and it either does not work at all, or it only sums based on one condition but not the other... I am really clueless on how to do this. It would be easy to just do it in excel with an if function but I have to use python..

def rates():
    for i in range(df['Currency']):
        if i== 'GBP':
            df['D'] = df['A']+ df['C']
        elif i== 'EUR':
            df['D'] = df['B']+ df['C']
        else: 


        rates() 
Oerp
  • 1
  • 1

1 Answers1

0
import numpy as np
df['D'] = df['C'] + np.where(df['Currency']=='GBP', df['B'], df['A'])
qaziqarta
  • 1,782
  • 1
  • 4
  • 11
  • I have done that. The only problem now is if I do this for EUR, it deletes out the values that were added when I done it for GBp. – Oerp Aug 12 '22 at 13:24
  • Yeah, I misunderstood your question - thought there are only two choices, GBP/EUR. For your problem this answer would be good: https://stackoverflow.com/a/19913845/19032206 – qaziqarta Aug 12 '22 at 18:03