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
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.
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
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.