Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cffca8e

Browse files
authored
feat: add pandas solutions to lc problems: No.0197,0262 (#1876)
1 parent f383454 commit cffca8e

File tree

6 files changed

+177
-0
lines changed

6 files changed

+177
-0
lines changed

‎solution/0100-0199/0197.Rising Temperature/README.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,18 @@ FROM
8787
ON SUBDATE(w1.recordDate, 1) = w2.recordDate AND w1.temperature > w2.temperature;
8888
```
8989

90+
### **Pandas**
91+
92+
```python
93+
import pandas as pd
94+
95+
96+
def rising_temperature(weather: pd.DataFrame) -> pd.DataFrame:
97+
weather.sort_values(by="recordDate", inplace=True)
98+
return weather[
99+
(weather.temperature.diff() > 0) & (weather.recordDate.diff().dt.days == 1)
100+
][["id"]]
101+
102+
```
103+
90104
<!-- tabs:end -->

‎solution/0100-0199/0197.Rising Temperature/README_EN.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,18 @@ FROM
8080
ON SUBDATE(w1.recordDate, 1) = w2.recordDate AND w1.temperature > w2.temperature;
8181
```
8282

83+
### **Pandas**
84+
85+
```python
86+
import pandas as pd
87+
88+
89+
def rising_temperature(weather: pd.DataFrame) -> pd.DataFrame:
90+
weather.sort_values(by="recordDate", inplace=True)
91+
return weather[
92+
(weather.temperature.diff() > 0) & (weather.recordDate.diff().dt.days == 1)
93+
][["id"]]
94+
95+
```
96+
8397
<!-- tabs:end -->
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pandas as pd
2+
3+
4+
def rising_temperature(weather: pd.DataFrame) -> pd.DataFrame:
5+
weather.sort_values(by="recordDate", inplace=True)
6+
return weather[
7+
(weather.temperature.diff() > 0) & (weather.recordDate.diff().dt.days == 1)
8+
][["id"]]

‎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)

AltStyle によって変換されたページ (->オリジナル) /