Why calling .sort() function on Pandas Series sorts its values in-place and returns nothing?
.sort()
sorts in-place.
That means that after you call .sort()
, your existing array has been sorted. It doesn't return anything.
To take an example from "core" Python:
In [175]: L = [2, 3, 1, 5]
In [176]: L.sort()
In [177]: print(L)
[1, 2, 3, 5]
It's the same for Pandas, as documented by Pandas.sort:
Sort values and index labels by value, in place. For compatibility with ndarray API. No return value
See also: What's the difference between Series.sort() and Series.order()?
Why does "return list.sort()" return None, not the list?
list.sort
sorts the list in place, i.e. it doesn't return a new list. Just write
newList.sort()
return newList
Dataframe sort future warning - trying to sort by sort_values()
Your problem is here:
OutputDf = OutputDf.sort_values("Date", ascending = True,
inplace = True)
When you're setting the parameter inplace=True, it's making the changes on the actual object that's calling the function, and it returns None.
So basically what you're doing is, you're sorting your dataframe, and then initializing the return value of None back to itself.
You can fix this by removing inplace=True
sorting in pandas not giving expected results
That is because when you access df.name[0]
, it points to the index of the row (check out by calling df.index
). The index value of the row where name is jack
is 0
, since when you created the df the first instance was jack
.
In order to access the first row on the sorted df, use .iloc
for positional indexing (or .loc
if you want a label-based indexing).
import pandas as pd
df = pd.DataFrame({'score' :[1,5,7,999], 'name':['jack','jill','chris','kevin']})
df.sort_values(by= 'score', ascending=False, inplace=True)
df
df.name.iloc[0]
This returns kevin
.
Why does '.sort()' cause the list to be 'None' in Python?
Simply remove the assignment from
result = result.sort()
leaving just
result.sort()
The sort
method works in-place (it modifies the existing list), so it returns None
. When you assign its result to the name of the list, you're assigning None
. So no assignment is necessary.
But in any case, what you're trying to accomplish can easily (and more efficiently) be written as a one-liner:
max(len(Ancestors(T,x)) for x in OrdLeaves(T))
max
operates in linear time, O(n), while sorting is O(nlogn). You also don't need nested list comprehensions, a single generator expression will do.
Sorting a pandas series
Use sort_values
, i.e. means = means.sort_values()
. [Pandas v0.17+]
(Very old answer, pre-v0.17 / 2015)
pandas used to use order()
method: means = means.order()
.
Related Topics
Turn String into a List and Remove Carriage Returns (Python)
Is There an Easy Way in Python to Wait Until Certain Condition Is True
Python: How to Split a List Based on a Specific Element
How to Tell Python to Convert Integers into Words
Unit Testing a Method With No Return Value
Number of Common Letters in Two Strings
Python, Deleting All Files in a Folder Older Than X Days
Django Development Server, How to Stop It When It Run in Background
How to Send Keys to a Game I Am Playing,Using Python
Python Regex - Finding Phone Number
Sum Numbers of Each Row of a Matrix Python
No Matching Distribution Found for Tkinter
Import Error: Dll Load Failed in Jupyter Notebook But Working in .Py File
Xlsxwriter: How to Open an Existing Worksheet in My Workbook
How to Calculate Range Between the Dataframe Values Using Python
Pandas Update and Add Rows One Dataframe With Key Column in Another Dataframe