計算名次¶
在 pandas
中,我們可以使用 .rank()
來計算名次。
In [1]:
Copied!
import pandas as pd
import pandas as pd
In [2]:
Copied!
names = ["張月豐", "韓丹容", "劉育來", "李嘉瑜", "鄭大鈞", "劉初賢", "劉婉婷", "張政勳", "陳新泰", "張倩旭"]
scores = [30, 90, 80, 70, 90, 50, 60, 70, 85, 65]
names = ["張月豐", "韓丹容", "劉育來", "李嘉瑜", "鄭大鈞", "劉初賢", "劉婉婷", "張政勳", "陳新泰", "張倩旭"]
scores = [30, 90, 80, 70, 90, 50, 60, 70, 85, 65]
In [3]:
Copied!
df = pd.DataFrame({'姓名':names, '分數':scores})
df = pd.DataFrame({'姓名':names, '分數':scores})
In [7]:
Copied!
df.rank?
df.rank?
In [5]:
Copied!
df['average'] = df['分數'].rank(ascending=False)
df['min'] = df['分數'].rank(method="min", ascending=False)
df['max'] = df['分數'].rank(method="max", ascending=False)
df['first'] = df['分數'].rank(method="first", ascending=False)
df['dense'] = df['分數'].rank(method="dense", ascending=False)
df['average'] = df['分數'].rank(ascending=False)
df['min'] = df['分數'].rank(method="min", ascending=False)
df['max'] = df['分數'].rank(method="max", ascending=False)
df['first'] = df['分數'].rank(method="first", ascending=False)
df['dense'] = df['分數'].rank(method="dense", ascending=False)
In [6]:
Copied!
df
df
Out[6]:
姓名 | 分數 | average | min | max | first | dense | |
---|---|---|---|---|---|---|---|
0 | 張月豐 | 30 | 10.0 | 10.0 | 10.0 | 10.0 | 8.0 |
1 | 韓丹容 | 90 | 1.5 | 1.0 | 2.0 | 1.0 | 1.0 |
2 | 劉育來 | 80 | 4.0 | 4.0 | 4.0 | 4.0 | 3.0 |
3 | 李嘉瑜 | 70 | 5.5 | 5.0 | 6.0 | 5.0 | 4.0 |
4 | 鄭大鈞 | 90 | 1.5 | 1.0 | 2.0 | 2.0 | 1.0 |
5 | 劉初賢 | 50 | 9.0 | 9.0 | 9.0 | 9.0 | 7.0 |
6 | 劉婉婷 | 60 | 8.0 | 8.0 | 8.0 | 8.0 | 6.0 |
7 | 張政勳 | 70 | 5.5 | 5.0 | 6.0 | 6.0 | 4.0 |
8 | 陳新泰 | 85 | 3.0 | 3.0 | 3.0 | 3.0 | 2.0 |
9 | 張倩旭 | 65 | 7.0 | 7.0 | 7.0 | 7.0 | 5.0 |