I'm working with some data where the customer postcode data is invalid. As a result I'm not able to map the CountryISOCode to their postcode resulting in a NaN. However, I have noticed that for all CountryISOCodes with NaN, the CurrencyCode can provide me with enough to fix the problem for now.
I've gone to various Stackoverflow articles but I cannot find the solution to my problem. I've tried...
def func(row):
if row['CountryISOCode'] == np.nan & row['Currency'] == 'EUR':
return 'IRE'
elif row['CountryISOCode'] == np.nan & row['Currency'] == 'GBP':
return 'GBR'
else:
return row['CountryISOCode']
df['CountryISOCode'] = df.apply(func, axis=1)
and some other methods but to no avail...
Below I have provided a replication of the data I'm working with
import pandas as pd
import numpy as np
data = [
['Steve', 'Invalid Postcode', 'GBP', np.nan ],
['Robyn', 'Invalid Postcode', 'EUR', np.nan],
['James', 'Valid Postcode', 'GBP', 'GBR'],
['Halo', 'Invalid Postcode', 'EUR', np.nan],
['Jesus', 'Valid Postcode', 'GBP', 'GBR']
]
df = pd.DataFrame(columns=["Name", "PostCode", "CurrencyCode", "CountryISOCode"], data=data)
Essentially if I was working with SQL my code would be as follows.
IF countryISOCode IS NULL
AND currency = ‘GBP’
THEN CountryISOCode = ‘GBR’
ELSE
IF countryISOCode IS NULL
AND currency = ‘EUR
THEN CountryISOCode = ‘IRE’
ELSE countryISOCode
END
Any ideas?