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

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

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

# リスト

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

## 昇順ソート

### sortメソッド

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

### sorted関数

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

## 降順ソート

### sortメソッド

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

### sorted関数

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

# 辞書型

## key昇順ソート

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

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

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

```py
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降順ソート

```py
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昇順ソート

```py
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降順ソート

```py
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昇順ソート

```py
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降順ソート

```py
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昇順ソート

```py
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降順ソート

```py
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昇順

```py
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降順

```py
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昇順

```py
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降順

```py
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値指定昇順

```py
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値指定降順

```py
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指定

```py
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)](https://github.com/pydata/pandas/issues/10806)

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

# 参考
[Pythonの辞書(dict型)をvalue値でソート](http://blog.livedoor.jp/yawamen/archives/51492355.html)
[ソート HOW TO (document)](http://docs.python.jp/3.5/howto/sorting.html)
[Python Tips：dict（辞書）型の中身をソートしたい](http://www.lifewithpython.com/2013/01/python-sort-dicts.html)
[pandas.Series.sort_index](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.sort_index.html)
[pandas.Series.sort_values](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.sort_values.html)
[pandas.DataFrame.sort_index](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_index.html)
[pandas.DataFrame.sort_values](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html)