How to give rank on datetime column group by another column with userid in it

Question:

My columns are ID, User_created_date and Customer city
while user_created_date is in 2023-01-01 00:01:05+05:30 format
now I wanted to know when did the first user was created based on each city and give rank to them based on city

I tried first sort -> groupby and then rank but doesn’t gives me desire output

Asked By: Akshay Patil

||

Answers:

To obtain a defined rank:

import pandas as pd


if __name__ == "__main__":
    df = pd.DataFrame({
        "ID": ["ID1", "ID2", "ID3"],
        "date": ["2016-6-10 20:30:0", "2016-7-1 19:45:30", "2013-10-12 4:5:1"],
        "city": ["New York", "Boston", "New York"]
    })
    df["date"] = pd.to_datetime(df["date"], format="%Y-%d-%m %H:%M:%S")

    print(df.groupby("city").min()["date"].rank(ascending=True))

This outputs

city
Boston      2.0
New York    1.0
Name: date, dtype: float64

Here, we grouped by city, then minimized over the date, then ranked over the values obtained in ascending order.

Answered By: GregoirePelegrin
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.