Reputation: 7448
I have the following df
,
currency value
USD 1.0
EUR 2.0
RMB 3.0
and I get a dict
for containing exchange rates,
rates = {'GBP': 0.76, 'USD': 1.0, 'CNY': 6.6}
when I do
df['value'].div(df['currency'].map(rates))
how do I know/get which value in currency
did not get map to rates
, like a KeyError
, I tried
try:
df['amount'] = df.['value'].div(df['currency'].map(rates))
except KeyError as e:
print('Key {} does not exist in the exchange rates dictionary'.format(e.args[0]))
but no errors were thrown. I am wondering how to do that.
Upvotes: 1
Views: 686
Reputation: 862511
Your code working and if no key in dict get NaN
s in column value
.
So need all currency
with NaN
s after map
:
a = df['value'].div(df['currency'].map(rates))
b = df.loc[a.isnull(), 'currency']
print (b)
1 EUR
2 RMB
Name: currency, dtype: object
Upvotes: 1