I often use sorting, but there are some differences depending on the type, so I will summarize it. I will summarize each by list, dictionary type, Series, and DataFrame.
I think there are other ways to do it. If you have any recommendations, please let us know.
I think the sort method is the easiest. I think that you can respond flexibly if you also know the sorted method. Note that the sorted method does not make any changes to the list itself and returns the sorted list as a return value.
lst = [3, 5, 2, 9, 0, 4]
lst.sort()
print(lst)
#=> [0, 2, 3, 4, 5, 9]
lst = [3, 5, 2, 9, 0, 4]
lst = sorted(lst)
print(lst)
#=> [0, 2, 3, 4, 5, 9]
lst_rvs = [3, 5, 2, 9, 0, 4]
lst_rvs.sort()
lst_rvs.reverse()
print(lst_rvs)
#=> [9, 5, 4, 3, 2, 0]
lst = [3, 5, 2, 9, 0, 4]
lst = sorted(lst, key=lambda x: -x)
print(lst)
#=> [9, 5, 4, 3, 2, 0]
Use the sorted function that was also used in the list. One thing to note about the dictionary type is that the list is returned as a return value.
dct = { 2: 3, 3: 4, 1: 2, 0: 8, 4: 2 }
dct = sorted(dct.items())
print(dct)
#=> [(0, 8), (1, 2), (2, 3), (3, 4), (4, 2)]
I think it's easy to imagine using it as follows.
dct = { 2: 3, 3: 4, 1: 2, 0: 8, 4: 2 }
for k, v in sorted(dct.items()):
print(str(k) + ": " + str(v))
0: 8
1: 2
2: 3
3: 4
4: 2
Hereafter, the dictionary type will be explained in the output format of the for statement.
dct = {2: 3, 3: 4, 1: 2, 0: 8, 4: 2}
for k, v in sorted(dct.items(), key=lambda x: -x[0]):
print(str(k) + ": " + str(v))
4: 2
3: 4
2: 3
1: 2
0: 8
dct = {2: 3, 3: 4, 1: 2, 0: 8, 4: 2}
for k, v in sorted(dct.items(), key=lambda x: x[1]):
print(str(k) + ": " + str(v))
1: 2
4: 2
2: 3
3: 4
0: 8
dct = {2: 3, 3: 4, 1: 2, 0: 8, 4: 2}
for k, v in sorted(dct.items(), key=lambda x: -x[1]):
print(str(k) + ": " + str(v))
0: 8
3: 4
2: 3
1: 2
4: 2
Series (pandas)
from pandas import Series
ser = Series({2: 3, 3: 4, 1: 2, 0: 8, 4: 2})
ser = ser.sort_index()
print(ser)
0 8
1 2
2 3
3 4
4 2
dtype: int64
from pandas import Series
ser = Series({2: 3, 3: 4, 1: 2, 0: 8, 4: 2})
ser = ser.sort_index(ascending=False)
print(ser)
4 2
3 4
2 3
1 2
0 8
dtype: int64
from pandas import Series
ser = Series({2: 3, 3: 4, 1: 2, 0: 8, 4: 2})
ser = ser.sort_values()
print(ser)
1 2
4 2
2 3
3 4
0 8
dtype: int64
from pandas import Series
ser = Series({2: 3, 3: 4, 1: 2, 0: 8, 4: 2})
ser = ser.sort_values(ascending=False)
print(ser)
0 8
3 4
2 3
4 2
1 2
dtype: int64
DataFrame (pandas)
from pandas import DataFrame
df = DataFrame([[3, 4, 2, 8, 2, 3], [3, 5, 1, 9, 4, 2], [9, 3, 1, 6, 3, 3]], index=[2,3,1], columns=["HP", "Attack", "Defence", "Special Attack", "Special Defence", "Speed"])
print(df)
df = df.sort_index()
print(df)
HP Attack Defence Special Attack Special Defence Speed
2 3 4 2 8 2 3
3 3 5 1 9 4 2
1 9 3 1 6 4 3
HP Attack Defence Special Attack Special Defence Speed
1 9 3 1 6 4 3
2 3 4 2 8 2 3
3 3 5 1 9 4 2
from pandas import DataFrame
df = DataFrame([[3, 4, 2, 8, 2, 3], [3, 5, 1, 9, 4, 2], [9, 3, 1, 6, 3, 3]], index=[2,3,1], columns=["HP", "Attack", "Defence", "Special Attack", "Special Defence", "Speed"])
print(df)
df = df.sort_index(ascending=False)
print(df)
HP Attack Defence Special Attack Special Defence Speed
2 3 4 2 8 2 3
3 3 5 1 9 4 2
1 9 3 1 6 3 3
HP Attack Defence Special Attack Special Defence Speed
3 3 5 1 9 4 2
2 3 4 2 8 2 3
1 9 3 1 6 3 3
from pandas import DataFrame
df = DataFrame([[3, 4, 2, 8, 2, 3], [3, 5, 1, 9, 4, 2], [9, 3, 1, 6, 3, 3]], index=[2,3,1], columns=["HP", "Attack", "Defence", "Special Attack", "Special Defence", "Speed"])
print(df)
df = df.sort_index(axis=1)
print(df)
HP Attack Defence Special Attack Special Defence Speed
2 3 4 2 8 2 3
3 3 5 1 9 4 2
1 9 3 1 6 4 3
Attack Defence HP Special Attack Special Defence Speed
2 4 2 3 8 2 3
3 5 1 3 9 4 2
1 3 1 9 6 4 3
from pandas import DataFrame
df = DataFrame([[3, 4, 2, 8, 2, 3], [3, 5, 1, 9, 4, 2], [9, 3, 1, 6, 3, 3]], index=[2,3,1], columns=["HP", "Attack", "Defence", "Special Attack", "Special Defence", "Speed"])
print(df)
df = df.sort_index(axis=1, ascending=False)
print(df)
HP Attack Defence Special Attack Special Defence Speed
2 3 4 2 8 2 3
3 3 5 1 9 4 2
1 9 3 1 6 3 3
Speed Special Defence Special Attack HP Defence Attack
2 3 2 8 3 2 4
3 2 4 9 3 1 5
1 3 3 6 9 1 3
from pandas import DataFrame
df = DataFrame([[3, 4, 2, 8, 2, 3], [3, 5, 1, 9, 4, 2], [9, 3, 1, 6, 3, 3]], index=[2,3,1], columns=["HP", "Attack", "Defence", "Special Attack", "Special Defence", "Speed"])
print(df)
df = df.sort_values(by="Special Defence")
print(df)
HP Attack Defence Special Attack Special Defence Speed
2 3 4 2 8 2 3
3 3 5 1 9 4 2
1 9 3 1 6 3 3
HP Attack Defence Special Attack Special Defence Speed
2 3 4 2 8 2 3
1 9 3 1 6 3 3
3 3 5 1 9 4 2
from pandas import DataFrame
df = DataFrame([[3, 4, 2, 8, 2, 3], [3, 5, 1, 9, 4, 2], [9, 3, 1, 6, 3, 3]], index=[2,3,1], columns=["HP", "Attack", "Defence", "Special Attack", "Special Defence", "Speed"])
print(df)
df = df.sort_values(by="Special Defence", ascending=False)
print(df)
HP Attack Defence Special Attack Special Defence Speed
2 3 4 2 8 2 3
3 3 5 1 9 4 2
1 9 3 1 6 3 3
HP Attack Defence Special Attack Special Defence Speed
3 3 5 1 9 4 2
1 9 3 1 6 3 3
2 3 4 2 8 2 3
from pandas import DataFrame
df = DataFrame([[3, 4, 2, 8, 2, 3], [3, 5, 1, 9, 4, 2], [9, 3, 1, 6, 3, 3]], index=[2, 3, 1], columns=["HP", "Attack", "Defence", "Special Attack", "Special Defence", "Speed"])
print(df)
df = df.sort_values(by=3, axis=1)
print(df)
ValueError: When sorting by column, axis must be 0 (rows)
I could do it with this, but I can't. .. BUG/ENH: sort_values(by=index_label, axis=1)
I see, I can't do it now. It seems that it will be possible with the next major version upgrade. Scheduled date August 31, 2017 (as of July 15, 2016)
Sort Python dictionary (dict type) by value value Sort HOW TO (document) Python Tips: I want to sort the contents of a dict (dictionary) type pandas.Series.sort_index pandas.Series.sort_values pandas.DataFrame.sort_index pandas.DataFrame.sort_values
Recommended Posts