Sort Series With Pandas In Python
I selected one column from the a DataFrame, then I got a Series. How can I sort the series? I used the Series.sort(), but it doesn't work. df = pd.DataFrame({'A': [5,0,3,8],
Solution 1:
One of them will work for you -
df.sort('A',ascending=False,inplace=True) #old version
df.sort_values('A',ascending=False,inplace=True) #new version
Solution 2:
But there is no result returned.
That is because the sort is in place, it modifies the object. Try this
A = df['A'].copy()
A.sort()
print(A)
Solution 3:
Since the sort()
function is deprecated, one must use the sort_values(inplace=True)
for inplace sorting instead (source).
So the code should look like this:
A = df['A'].copy()
A.sort_values(inplace=True)
Post a Comment for "Sort Series With Pandas In Python"