简体   繁体   中英

Create new pandas column based on categorical values in other column (python)

I have a data frame with country and traffic columns:

Country    |   Traffic
  US            8687
  Italy         902834
  Germany       2343
  Brazil        4254
  France        23453

I want to add a third column called "Region" to this data frame. It would look like this:

 Country    |   Traffic   | Region
  US            8687         US
  Italy         902834       EU
  Germany       2343         EU
  Brazil        4254         LA
  France        23453        EU

The following code works if I have only two Regions. I am looking more for an if/else , map , or lambda statement:

df['Region'] = np.where(df['Country'] == 'US', 'US', 'EU')

Thank You.

You could use a dictionary:

region_from_country = {
    'US': 'US', 
    'Italy': 'EU',
    'Germany': 'EU',
    'Brazil': 'LA', 
    'France': 'EU',
}
df['Region'] = df['Country'].replace(region_from_country)

The keys in the dictionary are the countries and the values are the corresponding regions.

One simple approach is this:

dict ={'US':'US','Italy':'EU','Germany':'EU','Brazil':'LA','France':'EU'}

df['Region']=df['Country'].apply(lambda x : dict[x])

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM