Leap Abroad
Leap Abroad

Reputation: 1

add new column in Pandas Data frame with categories

I have the following wine classification data:

country, price, colour
USA      10     white
italy    25     red
USA      75     rose
Germany  125    white 

I have to add a new column in pandas based on the information below:

New column Name is 'Affordability':

Cheap if price < 50 or expensive if price is >50 but Price <100 or very exp if price >100

Upvotes: 0

Views: 155

Answers (1)

olv1do
olv1do

Reputation: 96

You can use numpy.select to add an extra column with conditions:

import numpy as np 
condlst = [df['price_column'] < 50,
           df['price_column'] < 100,
           df['price_column'] >= 100]
choicelst = ['Cheap',
             'Expensive',
             'Very Expensive',]
df['Affordability'] = np.select(condlist, choicelst)

Please note:

  • the number of conditions should always match the number of choices.
  • conditions provided here are self excluding and will be checked in the order inserted so first it will check if the price is below 50 and if not will check if the price is below 100 and so on. This way you save the number of checks you need to do.

More to read about np.select here.

Upvotes: 1

Related Questions