0

I want to sort out the column of the pandas dataframe which (includes floating numbers) by the highest negative values first. for example i have df["values"]=[0.00378,00878,-0.001,-0.00487] thanks

Zaman Azam
  • 39
  • 8
  • Does this answer your question? [python, sort descending dataframe with pandas](https://stackoverflow.com/questions/24988873/python-sort-descending-dataframe-with-pandas) – roganjosh Sep 16 '20 at 22:48
  • it give positive number first. i need negatives. @roganjosh – Zaman Azam Sep 16 '20 at 23:13

1 Answers1

0
df["values"].sort_values(reverse=True)

^ This will sort the list in the descending order (largest number to smallest)

df["values"]. sort_values()

^ This will sort the list in the ascending order (smallest number to largest)

The smallest numbers there include negatives. However, if you're trying to only sort through negative numbers, you can filter the list for <0 and then sort in the second approach listed here (without including the reverse parameter).

j413254
  • 173
  • 1
  • 10
  • more info here: https://www.programiz.com/python-programming/methods/list/sort – j413254 Sep 16 '20 at 23:00
  • getting error "series" object has not attribute "sort". – Zaman Azam Sep 16 '20 at 23:09
  • @ZamanAzam looks like sort() was deprecated and is now sort_values(). Give that a try. I updated the original answer. Hope that helps. – j413254 Sep 16 '20 at 23:25
  • I don't think this has ever worked. `ascending=False` is the argument. Why are you guessing? – roganjosh Sep 17 '20 at 00:41
  • @roganjosh it does work. I wasn't guessing. Here's more info for you to review. https://docs.python.org/3/howto/sorting.html – j413254 Sep 18 '20 at 15:22
  • That's for python _lists_. This is a Series – roganjosh Sep 18 '20 at 21:15
  • 1
    Yes that is for lists. If you want to sort a series, it uses Series.sort_values(axis=0, ascending=True, inplace=False, kind=’quicksort’, na_position=’last’) I think we are saying the same thing and we agree on the differences between series and list. More details here for zaman to review https://www.geeksforgeeks.org/python-pandas-series-sort_values/amp – j413254 Sep 20 '20 at 02:13