148
140

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python sortのまとめ (リスト、辞書型、Series、DataFrame)

Last updated at Posted at 2016-07-16

ソートはよく使いますが型によってちょこちょこ違うところがあるのでまとめます。
リスト、辞書型、Series、DataFrameでそれぞれまとめようと思います。

やり方は他にもあると思います。おすすめがあれば是非ご教示願います。

リスト

sortメソッドが一番楽だと思います。sortedメソッドも知っておくと柔軟に対応出来ると思います。
sortedメソッドはリスト自体に変更は起こさず返り値として並び替え後のリストを返すことに注意です。

昇順ソート

sortメソッド

lst = [3, 5, 2, 9, 0, 4]
lst.sort()
print(lst)
#=> [0, 2, 3, 4, 5, 9]

sorted関数

lst = [3, 5, 2, 9, 0, 4]
lst = sorted(lst)
print(lst)
#=> [0, 2, 3, 4, 5, 9]

降順ソート

sortメソッド

lst_rvs = [3, 5, 2, 9, 0, 4]
lst_rvs.sort()
lst_rvs.reverse()
print(lst_rvs)
#=> [9, 5, 4, 3, 2, 0]

sorted関数

lst = [3, 5, 2, 9, 0, 4]
lst = sorted(lst, key=lambda x: -x)
print(lst)
#=> [9, 5, 4, 3, 2, 0]

辞書型

key昇順ソート

リストでも使ったsorted関数を使います。辞書型で注意が必要なのは返り値としてリストが返ってくることです。

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)]

以下のように使うのがイメージしやすいと思います。

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

以降辞書型はfor文での出力形式で説明します。

key降順ソート

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

value昇順ソート

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

value降順ソート

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)

index昇順ソート

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

index降順ソート

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

value昇順ソート

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

value降順ソート

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)

labelでのソート

index昇順

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

index降順

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

column昇順

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

column降順

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

valueでのソート

column値指定昇順

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

column値指定降順

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

index指定

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)

これで出来そうだったのに出来ない。。
BUG/ENH: sort_values(by=index_label, axis=1)

なるほど、今は出来ないらしい。次のメジャーバージョンアップで出来るようになるようです。
予定日 August 31, 2017 (2016 7/15現在)

参考

Pythonの辞書(dict型)をvalue値でソート
ソート HOW TO (document)
Python Tips:dict(辞書)型の中身をソートしたい
pandas.Series.sort_index
pandas.Series.sort_values
pandas.DataFrame.sort_index
pandas.DataFrame.sort_values

148
140
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
148
140

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?