diff --git a/solution/0100-0199/0197.Rising Temperature/README.md b/solution/0100-0199/0197.Rising Temperature/README.md index c7f89a1be25ce..bd685ffa0736d 100644 --- a/solution/0100-0199/0197.Rising Temperature/README.md +++ b/solution/0100-0199/0197.Rising Temperature/README.md @@ -87,4 +87,18 @@ FROM ON SUBDATE(w1.recordDate, 1) = w2.recordDate AND w1.temperature > w2.temperature; ``` +### **Pandas** + +```python +import pandas as pd + + +def rising_temperature(weather: pd.DataFrame) -> pd.DataFrame: + weather.sort_values(by="recordDate", inplace=True) + return weather[ + (weather.temperature.diff() > 0) & (weather.recordDate.diff().dt.days == 1) + ][["id"]] + +``` + diff --git a/solution/0100-0199/0197.Rising Temperature/README_EN.md b/solution/0100-0199/0197.Rising Temperature/README_EN.md index a346c9db29333..97c3c75a8fabc 100644 --- a/solution/0100-0199/0197.Rising Temperature/README_EN.md +++ b/solution/0100-0199/0197.Rising Temperature/README_EN.md @@ -80,4 +80,18 @@ FROM ON SUBDATE(w1.recordDate, 1) = w2.recordDate AND w1.temperature > w2.temperature; ``` +### **Pandas** + +```python +import pandas as pd + + +def rising_temperature(weather: pd.DataFrame) -> pd.DataFrame: + weather.sort_values(by="recordDate", inplace=True) + return weather[ + (weather.temperature.diff() > 0) & (weather.recordDate.diff().dt.days == 1) + ][["id"]] + +``` + diff --git a/solution/0100-0199/0197.Rising Temperature/Solution.py b/solution/0100-0199/0197.Rising Temperature/Solution.py new file mode 100644 index 0000000000000..0dcb7fe533075 --- /dev/null +++ b/solution/0100-0199/0197.Rising Temperature/Solution.py @@ -0,0 +1,8 @@ +import pandas as pd + + +def rising_temperature(weather: pd.DataFrame) -> pd.DataFrame: + weather.sort_values(by="recordDate", inplace=True) + return weather[ + (weather.temperature.diff() > 0) & (weather.recordDate.diff().dt.days == 1) + ][["id"]] diff --git a/solution/0200-0299/0262.Trips and Users/README.md b/solution/0200-0299/0262.Trips and Users/README.md index 11cdb315a09d9..c25fa534548f1 100644 --- a/solution/0200-0299/0262.Trips and Users/README.md +++ b/solution/0200-0299/0262.Trips and Users/README.md @@ -140,4 +140,53 @@ WHERE request_at BETWEEN '2013-10-01' AND '2013-10-03' GROUP BY request_at; ``` +### **Pandas** + +```python +import pandas as pd + + +def trips_and_users(trips: pd.DataFrame, users: pd.DataFrame) -> pd.DataFrame: + # 1) temporal filtering + trips = trips[trips["request_at"].between("2013-10-01", "2013-10-03")].rename( + columns={"request_at": "Day"} + ) + + # 2) filtering based not banned + # 2.1) mappning the column 'banned' to `client_id` and `driver_id` + df_client = ( + pd.merge(trips, users, left_on="client_id", right_on="users_id", how="left") + .drop(["users_id", "role"], axis=1) + .rename(columns={"banned": "banned_client"}) + ) + df_driver = ( + pd.merge(trips, users, left_on="driver_id", right_on="users_id", how="left") + .drop(["users_id", "role"], axis=1) + .rename(columns={"banned": "banned_driver"}) + ) + df = pd.merge( + df_client, + df_driver, + left_on=["id", "driver_id", "client_id", "city_id", "status", "Day"], + right_on=["id", "driver_id", "client_id", "city_id", "status", "Day"], + how="left", + ) + # 2.2) filtering based on not banned + df = df[(df["banned_client"] == "No") & (df["banned_driver"] == "No")] + + # 3) counting the cancelled and total trips per day + df["status_cancelled"] = df["status"].str.contains("cancelled") + df = df[["Day", "status_cancelled"]] + df = df.groupby("Day").agg( + {"status_cancelled": [("total_cancelled", "sum"), ("total", "count")]} + ) + df.columns = df.columns.droplevel() + df = df.reset_index() + + # 4) calculating the ratio + df["Cancellation Rate"] = (df["total_cancelled"] / df["total"]).round(2) + return df[["Day", "Cancellation Rate"]] + +``` + diff --git a/solution/0200-0299/0262.Trips and Users/README_EN.md b/solution/0200-0299/0262.Trips and Users/README_EN.md index 440adce8e9de5..87762ba27cc4b 100644 --- a/solution/0200-0299/0262.Trips and Users/README_EN.md +++ b/solution/0200-0299/0262.Trips and Users/README_EN.md @@ -127,4 +127,53 @@ WHERE request_at BETWEEN '2013-10-01' AND '2013-10-03' GROUP BY request_at; ``` +### **Pandas** + +```python +import pandas as pd + + +def trips_and_users(trips: pd.DataFrame, users: pd.DataFrame) -> pd.DataFrame: + # 1) temporal filtering + trips = trips[trips["request_at"].between("2013-10-01", "2013-10-03")].rename( + columns={"request_at": "Day"} + ) + + # 2) filtering based not banned + # 2.1) mappning the column 'banned' to `client_id` and `driver_id` + df_client = ( + pd.merge(trips, users, left_on="client_id", right_on="users_id", how="left") + .drop(["users_id", "role"], axis=1) + .rename(columns={"banned": "banned_client"}) + ) + df_driver = ( + pd.merge(trips, users, left_on="driver_id", right_on="users_id", how="left") + .drop(["users_id", "role"], axis=1) + .rename(columns={"banned": "banned_driver"}) + ) + df = pd.merge( + df_client, + df_driver, + left_on=["id", "driver_id", "client_id", "city_id", "status", "Day"], + right_on=["id", "driver_id", "client_id", "city_id", "status", "Day"], + how="left", + ) + # 2.2) filtering based on not banned + df = df[(df["banned_client"] == "No") & (df["banned_driver"] == "No")] + + # 3) counting the cancelled and total trips per day + df["status_cancelled"] = df["status"].str.contains("cancelled") + df = df[["Day", "status_cancelled"]] + df = df.groupby("Day").agg( + {"status_cancelled": [("total_cancelled", "sum"), ("total", "count")]} + ) + df.columns = df.columns.droplevel() + df = df.reset_index() + + # 4) calculating the ratio + df["Cancellation Rate"] = (df["total_cancelled"] / df["total"]).round(2) + return df[["Day", "Cancellation Rate"]] + +``` + diff --git a/solution/0200-0299/0262.Trips and Users/Solution.py b/solution/0200-0299/0262.Trips and Users/Solution.py new file mode 100644 index 0000000000000..2558b24f0a130 --- /dev/null +++ b/solution/0200-0299/0262.Trips and Users/Solution.py @@ -0,0 +1,43 @@ +import pandas as pd + + +def trips_and_users(trips: pd.DataFrame, users: pd.DataFrame) -> pd.DataFrame: + # 1) temporal filtering + trips = trips[trips["request_at"].between("2013-10-01", "2013-10-03")].rename( + columns={"request_at": "Day"} + ) + + # 2) filtering based not banned + # 2.1) mappning the column 'banned' to `client_id` and `driver_id` + df_client = ( + pd.merge(trips, users, left_on="client_id", right_on="users_id", how="left") + .drop(["users_id", "role"], axis=1) + .rename(columns={"banned": "banned_client"}) + ) + df_driver = ( + pd.merge(trips, users, left_on="driver_id", right_on="users_id", how="left") + .drop(["users_id", "role"], axis=1) + .rename(columns={"banned": "banned_driver"}) + ) + df = pd.merge( + df_client, + df_driver, + left_on=["id", "driver_id", "client_id", "city_id", "status", "Day"], + right_on=["id", "driver_id", "client_id", "city_id", "status", "Day"], + how="left", + ) + # 2.2) filtering based on not banned + df = df[(df["banned_client"] == "No") & (df["banned_driver"] == "No")] + + # 3) counting the cancelled and total trips per day + df["status_cancelled"] = df["status"].str.contains("cancelled") + df = df[["Day", "status_cancelled"]] + df = df.groupby("Day").agg( + {"status_cancelled": [("total_cancelled", "sum"), ("total", "count")]} + ) + df.columns = df.columns.droplevel() + df = df.reset_index() + + # 4) calculating the ratio + df["Cancellation Rate"] = (df["total_cancelled"] / df["total"]).round(2) + return df[["Day", "Cancellation Rate"]]