Skip to content

Commit ebeaba3

Browse files
committed
Solution for 262.
1 parent ff05136 commit ebeaba3

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

solution/0200-0299/0262.Trips and Users/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,53 @@ WHERE request_at BETWEEN '2013-10-01' AND '2013-10-03'
140140
GROUP BY request_at;
141141
```
142142

143+
### **Pandas**
144+
145+
```python
146+
import pandas as pd
147+
148+
149+
def trips_and_users(trips: pd.DataFrame, users: pd.DataFrame) -> pd.DataFrame:
150+
# 1) temporal filtering
151+
trips = trips[trips["request_at"].between("2013-10-01", "2013-10-03")].rename(
152+
columns={"request_at": "Day"}
153+
)
154+
155+
# 2) filtering based not banned
156+
# 2.1) mappning the column 'banned' to `client_id` and `driver_id`
157+
df_client = (
158+
pd.merge(trips, users, left_on="client_id", right_on="users_id", how="left")
159+
.drop(["users_id", "role"], axis=1)
160+
.rename(columns={"banned": "banned_client"})
161+
)
162+
df_driver = (
163+
pd.merge(trips, users, left_on="driver_id", right_on="users_id", how="left")
164+
.drop(["users_id", "role"], axis=1)
165+
.rename(columns={"banned": "banned_driver"})
166+
)
167+
df = pd.merge(
168+
df_client,
169+
df_driver,
170+
left_on=["id", "driver_id", "client_id", "city_id", "status", "Day"],
171+
right_on=["id", "driver_id", "client_id", "city_id", "status", "Day"],
172+
how="left",
173+
)
174+
# 2.2) filtering based on not banned
175+
df = df[(df["banned_client"] == "No") & (df["banned_driver"] == "No")]
176+
177+
# 3) counting the cancelled and total trips per day
178+
df["status_cancelled"] = df["status"].str.contains("cancelled")
179+
df = df[["Day", "status_cancelled"]]
180+
df = df.groupby("Day").agg(
181+
{"status_cancelled": [("total_cancelled", "sum"), ("total", "count")]}
182+
)
183+
df.columns = df.columns.droplevel()
184+
df = df.reset_index()
185+
186+
# 4) calculating the ratio
187+
df["Cancellation Rate"] = (df["total_cancelled"] / df["total"]).round(2)
188+
return df[["Day", "Cancellation Rate"]]
189+
190+
```
191+
143192
<!-- tabs:end -->

solution/0200-0299/0262.Trips and Users/README_EN.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,53 @@ WHERE request_at BETWEEN '2013-10-01' AND '2013-10-03'
127127
GROUP BY request_at;
128128
```
129129

130+
### **Pandas**
131+
132+
```python
133+
import pandas as pd
134+
135+
136+
def trips_and_users(trips: pd.DataFrame, users: pd.DataFrame) -> pd.DataFrame:
137+
# 1) temporal filtering
138+
trips = trips[trips["request_at"].between("2013-10-01", "2013-10-03")].rename(
139+
columns={"request_at": "Day"}
140+
)
141+
142+
# 2) filtering based not banned
143+
# 2.1) mappning the column 'banned' to `client_id` and `driver_id`
144+
df_client = (
145+
pd.merge(trips, users, left_on="client_id", right_on="users_id", how="left")
146+
.drop(["users_id", "role"], axis=1)
147+
.rename(columns={"banned": "banned_client"})
148+
)
149+
df_driver = (
150+
pd.merge(trips, users, left_on="driver_id", right_on="users_id", how="left")
151+
.drop(["users_id", "role"], axis=1)
152+
.rename(columns={"banned": "banned_driver"})
153+
)
154+
df = pd.merge(
155+
df_client,
156+
df_driver,
157+
left_on=["id", "driver_id", "client_id", "city_id", "status", "Day"],
158+
right_on=["id", "driver_id", "client_id", "city_id", "status", "Day"],
159+
how="left",
160+
)
161+
# 2.2) filtering based on not banned
162+
df = df[(df["banned_client"] == "No") & (df["banned_driver"] == "No")]
163+
164+
# 3) counting the cancelled and total trips per day
165+
df["status_cancelled"] = df["status"].str.contains("cancelled")
166+
df = df[["Day", "status_cancelled"]]
167+
df = df.groupby("Day").agg(
168+
{"status_cancelled": [("total_cancelled", "sum"), ("total", "count")]}
169+
)
170+
df.columns = df.columns.droplevel()
171+
df = df.reset_index()
172+
173+
# 4) calculating the ratio
174+
df["Cancellation Rate"] = (df["total_cancelled"] / df["total"]).round(2)
175+
return df[["Day", "Cancellation Rate"]]
176+
177+
```
178+
130179
<!-- tabs:end -->
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import pandas as pd
2+
3+
4+
def trips_and_users(trips: pd.DataFrame, users: pd.DataFrame) -> pd.DataFrame:
5+
# 1) temporal filtering
6+
trips = trips[trips["request_at"].between("2013-10-01", "2013-10-03")].rename(
7+
columns={"request_at": "Day"}
8+
)
9+
10+
# 2) filtering based not banned
11+
# 2.1) mappning the column 'banned' to `client_id` and `driver_id`
12+
df_client = (
13+
pd.merge(trips, users, left_on="client_id", right_on="users_id", how="left")
14+
.drop(["users_id", "role"], axis=1)
15+
.rename(columns={"banned": "banned_client"})
16+
)
17+
df_driver = (
18+
pd.merge(trips, users, left_on="driver_id", right_on="users_id", how="left")
19+
.drop(["users_id", "role"], axis=1)
20+
.rename(columns={"banned": "banned_driver"})
21+
)
22+
df = pd.merge(
23+
df_client,
24+
df_driver,
25+
left_on=["id", "driver_id", "client_id", "city_id", "status", "Day"],
26+
right_on=["id", "driver_id", "client_id", "city_id", "status", "Day"],
27+
how="left",
28+
)
29+
# 2.2) filtering based on not banned
30+
df = df[(df["banned_client"] == "No") & (df["banned_driver"] == "No")]
31+
32+
# 3) counting the cancelled and total trips per day
33+
df["status_cancelled"] = df["status"].str.contains("cancelled")
34+
df = df[["Day", "status_cancelled"]]
35+
df = df.groupby("Day").agg(
36+
{"status_cancelled": [("total_cancelled", "sum"), ("total", "count")]}
37+
)
38+
df.columns = df.columns.droplevel()
39+
df = df.reset_index()
40+
41+
# 4) calculating the ratio
42+
df["Cancellation Rate"] = (df["total_cancelled"] / df["total"]).round(2)
43+
return df[["Day", "Cancellation Rate"]]

0 commit comments

Comments
 (0)