0

I make a dataset which is about bike .Price of bike included into this datasets.Now I would like to categoriz the price into "cheap","moderate" & "expensive" based on the bike prices.

data2.loc[data2["PRICE"] < 50000,'PRICE_CATEGORY'] = 'cheap'
data2.loc[data2["PRICE"] < 520000,'PRICE_CATEGORY'] = 'expensive'
data2.loc[data2["PRICE"] < 170000,'PRICE_CATEGORY'] = 'moderate'
data2.head()

'<' not supported between instances of 'str' and 'int' I have converted the "PRICE" into integer but the error shown

  • Can you post the output of `print(data2.dtypes)` ? – Nick ODell Apr 14 '23 at 19:58
  • print(data2.dtypes) BRAND object MODEL object BRAKING_SYSTEM object BRAKE_ISSUE int64 RUNNING_MILE object ENGINE_CONDITION int64 BATTERY_ISSUE int64 DENT_CONDITION object TIRE_CONDITIONS int64 SUSPENSION object MILAGE object BRAND_NEW_MARKET object PRICE object PRICE_CATEGORY object dtype: object – Md Faisal Uddin Apr 14 '23 at 20:04
  • 1
    "I have converted the "PRICE" into integer but the error shown" please post all your code including the code that does the conversion – cs95 Apr 14 '23 at 20:05
  • `PRICE object` Looks like price is still a string. – Nick ODell Apr 14 '23 at 20:08
  • 1
    Also, do you notice, price < 52000 will also match all rows for which price < 50000, meaning the order of your assignment is wrong. start with expensive, then moderate, then cheap. Or else make your conditions a little more foolproof (eg price < 50000 = cheap, 50000 <= price < 170000 = moderate, price >= 170000 = expensive) – cs95 Apr 14 '23 at 20:08
  • try `data2['PRICE'] = pd.to_numeric(data2['PRICE'], errors='coerce')` instead. Also see https://stackoverflow.com/questions/45273731/binning-a-column-with-pandas – cs95 Apr 14 '23 at 20:10
  • data2['PRICE'] = data2['PRICE'].astype(str).astype(int) – Md Faisal Uddin Apr 14 '23 at 20:10
  • You need to use [`pandas.cut`](https://pandas.pydata.org/docs/reference/api/pandas.cut.html) – mozway Apr 14 '23 at 20:11

0 Answers0