0
1

More than 3 years have passed since last update.

概要

変数に対して、
.sort_values(by="カラムorカラムのリスト", ascending=True
とする列の値が小さい順にソートされる。

ascending=Falseと指定すると大きい順になる。

リストの場合、順番が早いカラムが優先的にソートされる。

import numpy as np
import pandas as pd
np.random.seed(0)
columns = ["apple", "orange", "banana", "strawberry", "kiwifruit"]

#DF作成
df = pd.DataFrame()
display(df)

#ランダムに要素の数を作成
for colum in colums:
    df[colum] = np.random.choice(range(1, 11), 10)

#インデックスを設定
df.index = range(1, 11)

#第2引数にascendingを指定しない場合はascending=Trueとして処理される。
df = df.sort_values(by = columns)

display(df)

出力

image.png

columnsはリストなので順番が早いappleが優先されソートされる。

0
1
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
0
1