LoginSignup
1
2

More than 5 years have passed since last update.

Pythonで順位をつける

Last updated at Posted at 2018-01-31
test.py
import numpy as np
import pandas as pd
from scipy import stats
test= np.array([2,4,3,5,8,8,16])
df=pd.DataFrame(test, columns=(['data']))
df = df.assign(ranking=len(df.data)-stats.mstats.rankdata(df.data)+1)#順位を作成してdfに追加 rankだとデフォルトのメソッドがあるのでrankingにしといた。
df[['ranking', 'data']].sort_values('data',ascending=False)#順位は左端につけたいのでSELECTしてdataで並び替え

実行結果

index ranking test
6 1 16
4 2.5 8
5 2.5 8
3 4 5
1 5 4
2 6 3
0 7 2

参考URL
http://johshisha.hatenablog.com/entry/2017/04/05/200454

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