Home > front end >  How to add an ordinal data column based on prices to dataframe?
How to add an ordinal data column based on prices to dataframe?

Time:04-27

I have a dataset of used cars, there is a column of prices in the dataset. I want to introduce a new ordinal column with the values (high, medium, and low) considering the prices of cars like so:

price ordinal
higher than 20,000 high
10,000-20,000 medium
below 10,000 low

Dataset: screenshot of dataset

CodePudding user response:

You can try with cut

df['ordinal'] = pd.cut(df['price'],
                       [0,10000,20000,np.Inf],
                       labels = ['Low','Medium','High'])
 

CodePudding user response:

df["ordinal"] = df["price"].apply(lambda x: "high" if x > 20000 else "low" if x < 10000 else "medium")
  • Related