Skip to content

Solution for 197,262. #1876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions solution/0100-0199/0197.Rising Temperature/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]

```

<!-- tabs:end -->
14 changes: 14 additions & 0 deletions solution/0100-0199/0197.Rising Temperature/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]

```

<!-- tabs:end -->
8 changes: 8 additions & 0 deletions solution/0100-0199/0197.Rising Temperature/Solution.py
Original file line number Diff line number Diff line change
@@ -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"]]
49 changes: 49 additions & 0 deletions solution/0200-0299/0262.Trips and Users/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]

```

<!-- tabs:end -->
49 changes: 49 additions & 0 deletions solution/0200-0299/0262.Trips and Users/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]

```

<!-- tabs:end -->
43 changes: 43 additions & 0 deletions solution/0200-0299/0262.Trips and Users/Solution.py
Original file line number Diff line number Diff line change
@@ -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"]]