0
How to arrange numbers in ascending order in python using pandas or however you want
Let's say I have a list of Import pandas as pd Ages = [20, 50, 10, 60, 4, 2] Ages = pd.Series(ages) So how can I print ascending order of these ages list and create another empty list then append those in ascending order?
4 odpowiedzi
+ 4
a = [1,4,6,2,3,5,2]
b = sorted(a)
print(b)
print(b[::-1])
[1,2,2,3,4,5,6]
[6,5,4,3,2,2,1]
+ 1
Let me add to the existing answers.
If you want to sort a PANDAS DATAFRAME by one of its columns, use the following method (df is your dataframe):
df.sort_values(by = column_label, ascending = True/False, inplace = False/True)
Here column_label is the name/heading/label (a string; make sure to use double-inverted commas) of the column by whose values you want to arrange the rows of the dataframe.
Set ascending to False for descending order.
If you want to make changes (by arranging) to the original dataframe (and return None), set inplace to True; but if you just want to return the arranged dataframe into a new variable leaving the original one unaffected, set inplace to False.
I hope this helps.
+ 1
Example of sort_values method in Pandas:
Let the dataframe (df) be:
column1 column2
0 12 2
1 60 1
2 31 5
3 1 21
To order it BY the values of column2 NOT in ASCENDING order (i.e., descending order; ascending = True is default), return a new dataframe to df2 and not affect the original dataframe df (inplace = False is default):
df2 = df.sort_values(by = "column2", ascending = False)
But to order it by the values of column2 in ascending order and affecting the original dataframe df:
df.sort_values(by = "column2", inplace = True)
0
Thanks a lot