Home > Net >  Problems in sorting a column by value
Problems in sorting a column by value

Time:02-01

I was trying to sort the food from the most caloric to the less caloric, I have a 2-column dataset (with about 2000 different foods), with the Name of the food and the Energy in kcal of that food.

Name Energy, kcal
Fish 80
Meat 70
Oil 900

I tried with:

df_Kcal.sort_values(by='Energy, kcal',ascending=False).head(10)

But as a result, i have a list of foods from 99kcal (The highest value it's 900kcal)

Name Energy, kcal
Stockfish 99
Wine 96
lobster 93

If I do the same code, with .sample(10) I can see foods with more calories around the dataset

How can I sort it correctly?

Thank you

CodePudding user response:

The kcal values seem to be stored as string, which explains the result of sort (values are sorted alphabetically).

To fix this, use pandas.DataFrame.astype to set the type of the kcal column to a numeric type (eg. int):

df.astype({'Name': 'string', 'Energy, kcal': 'int32'})

The sorting should then yield the expected result.

  •  Tags:  
  • Related