I have df that has a column "Country" with country codes eg "DE" for Germany, "MX" for Mexico etc. I've created a function below and used.apply to create a new column "Region". I'm wondering if there is a more slick / efficient way to go about achieving this eg with np.where? Still trying to get my head around the syntax for np.where, the below solution works for now, just trying to broaden my knowledge of other ways to achieve this with Pandas:)
def region(df):
if df.Country == 'US':
return "NA"
elif df.Country == 'DE' or df.Country == 'ES' or df.Country == 'FR' or df.Country == 'GB' or df.Country == 'IT':
return "EMEA"
elif df.Country == 'IN':
return "APAC"
elif df.Country == 'BR' or df.Country == 'MX':
return "LATAM"
df.insert(2, 'Region', df.apply(region, axis=1))
One way to achieve this is to use dictionary with pandas.Series.map
function:
#Create a one-time dictionary with mapping of country against region
d = {'US':'NA','DE':'EMEA','ES':'EMEA','FR':'EMEA','GB':'EMEA','IT':'EMEA','IN':'APAC','BR':'LATAM','MX':'LATAM'}
#And use map function to create a new column
df['Region'] = df['Country'].map(d)
print(df)
Country Region
0 US NA
1 DE EMEA
2 ES EMEA
3 FR EMEA
4 GB EMEA
5 IT EMEA
6 IN APAC
7 BR LATAM
8 MX LATAM
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.