Tsallaka zuwa abun ciki
Justice Tech School

rank and shift

rank gives positions and shift moves values down for comparisons.

Example
df = pd.DataFrame({"name": ["Ada", "Kofi", "Amina", "Tunde"], "country": ["NG", "GH", "NG", "NG"], "score": [92, 78, 85, 71]})
df["rank"] = df["score"].rank(ascending=False).astype(int)
df["prev"] = df["score"].shift(1)
print(df[["name", "rank", "prev"]])

Result

    name  rank  prev
0    Ada     1   NaN
1   Kofi     3  92.0
2  Amina     2  78.0
3  Tunde     4  85.0

Exercise

Move down one row.

s.(1)

Quiz Test

What does rank give?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.