0

The following link is to the colab notebook I am using, the code I am talking about is the last one I have the following table which I have already put in descending order for the column "protein". What I need to have is those first 15 results (the highest 15 protein results) but now in ascending or descending order of the column "calories".

All I have for now is the following code for putting "protein" in descending order.

df1 = df.sort_values(by = ['protein', 'calories'], inplace = True, ascending = [False, True])
df.head(15)

This code will only put one column in ascending /desceding order.

Now that I have my column "protein" in desending order, so highest first, I need these first 15 rows and I need to get rid of the rest if possible and with these 15 I now need to put them in ascending /descending order of "calorie" but I have not been able to drop the rest of the data or sort it without.

Hope someone can help.. sorry for so much text. (first time)

I have tried fixing it with df1.head() but I get the following error: AttributeError: 'NoneType' object has no attribute 'head'

Thanks!!

Andrew
  • 5
  • 3
  • `i have the following table` - where is a table? – Алексей Р Oct 25 '22 at 19:11
  • Hi and welcome to stackoverflow. I concur that a table with (sample) input data and a table with your expected result would be useful to help. If I understand what you want correctly, the code you have is really close (except maybe for the typo `df.head` -> `df1.head`). But it is not possible to say more without more details. – maow Oct 25 '22 at 19:44
  • I am sorry I could not upload a photo, I have just added a link to the colab page I am using, the code I am stuck with is the very last one. I hope you can see if ok. Thanks – Andrew Oct 25 '22 at 19:59

1 Answers1

0

As noted by some of the comments, it looks like you have a typo in the lines below:

df1 = df.sort_values(by = ['protein', 'calories'], inplace = True, ascending = [False, True])
df.head(15)

In the line below, you define df1 as:

df1 = df.sort_values(by = ['protein', 'calories'], inplace = True, ascending = [False, True])

But in the following line, you reference df instead of df1

df.head(15)

You could try revising like below, also to remove the inplace = true argument to return a new list for the df1 value:

df1 = df.sort_values(by = ['protein', 'calories'], ascending = [False, True])
df1.head(15)

The thread below describes sorting a Pandas Data Frame by two or more columns, which may help:

How to sort a dataFrame in python pandas by two or more columns?

jrynes
  • 187
  • 1
  • 9
  • I tried what you suggested with the df1.head but I have got an attribute error: AttributeError: 'NoneType' object has no attribute 'head' @jrynes – Andrew Oct 26 '22 at 13:28
  • Try removing the inplace = true argument from where you set the value for df1 - That should cause the df.sort_values() function to return a new list When the list is sorted in place, it changes the value for df, but won't return a new list So the df1 value would not be set, and a 'None' element doesn't have the .head() attribute But if you remove that argument, it should return a new list, which we can set to the variable df1 And that new variable should have the argument .head() – jrynes Oct 27 '22 at 08:48