0

This seems very simple but I just can't figure it out.

I have a dataframe with an amount column in GBpence, Euro and USD. I need a new column with the amount in GBP, the only way I can think of doing this is to first convert the amount field to string, separate the amount from the currency sign and then use numpy to perform a conditional calculation.

df = pd.DataFrame({'date': ['2018-11-22','2018-11-23','2018-11-24'],
               'amount': ['3.80p','$4.50','\N{euro sign}3.40'], 
               'usd-gbp':['0.82','0.83','0.84'], 
               'eur-gbp':['0.91','0.92','0.93']})

enter image description here

I am trying to convert the amount column to string so I can extract the currency and float amount but it just converts all the rows into the same string.

df['new'] = str(df['amount'])

enter image description here

Expected output would just be the amount values in string format so I can perform slicing on them.

enter image description here

Any help would be appreciated. Thanks.

1
  • Please also include the expected output in your question. Commented Dec 3, 2021 at 10:56

1 Answer 1

1

You can use replace to replace currency symbol by '0.82*' and '0.91*' then evaluate the operation with pd.eval:

to_gbp = {'p': '*1', '\$': '0.82*', '€': '0.91*'}
df['gbp'] = pd.eval(df['amount'].replace(to_gbp, regex=True))
print(df)

# Output:
  amount usd-gbp eur-gbp    gbp
0  3.80p    0.82    0.91  3.800
1  $4.50    0.82    0.91  3.690
2  €3.40    0.82    0.91  3.094

Detail about replace:

>>> df['amount'].replace(to_gbp, regex=True)
0       3.80*1
1    0.82*4.50
2    0.91*3.40
Name: amount, dtype: object

Update

I didnt mention that the exchange rates will differ based on the date. I have updated the question so show this. Is this still possible with 'replace'?

Create a custom function and apply it to each row:

def convert(row):
    d = {'$': 'usd-gbp', '€': 'eur-gbp'}
    c = row['amount'][0]
    return float(row['amount'][1:]) * float(row[d[c]]) \
               if c in d else float(row['amount'][:-1])

df['gbp'] = df.apply(convert, axis=1)
print(df)

# Output:
         date amount usd-gbp eur-gbp    gbp
0  2018-11-22  3.80p    0.82    0.91  3.800
1  2018-11-23  $4.50    0.83    0.92  3.735
2  2018-11-24  €3.40    0.84    0.93  3.162
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I was trying to simplify the problem for the post. I didnt mention that the exchange rates will differ based on the date. I have updated the question so show this. Is this still possible with 'replace'?
Glad to read that :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.