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 9d11a79

Browse files
authored
feat: add pandas solutions to lc problem: No.0175,0176 (#1858)
1 parent 29d4633 commit 9d11a79

File tree

6 files changed

+97
-0
lines changed

6 files changed

+97
-0
lines changed

‎solution/0100-0199/0175.Combine Two Tables/README.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,15 @@ FROM
9696
LEFT JOIN Address USING (personId);
9797
```
9898

99+
```pandas
100+
import pandas as pd
101+
102+
103+
def combine_two_tables(person: pd.DataFrame, address: pd.DataFrame) -> pd.DataFrame:
104+
return pd.merge(left=person, right=address, how="left", on="personId")[
105+
["firstName", "lastName", "city", "state"]
106+
]
107+
108+
```
109+
99110
<!-- tabs:end -->

‎solution/0100-0199/0175.Combine Two Tables/README_EN.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,15 @@ FROM
9292
LEFT JOIN Address USING (personId);
9393
```
9494

95+
```pandas
96+
import pandas as pd
97+
98+
99+
def combine_two_tables(person: pd.DataFrame, address: pd.DataFrame) -> pd.DataFrame:
100+
return pd.merge(left=person, right=address, how="left", on="personId")[
101+
["firstName", "lastName", "city", "state"]
102+
]
103+
104+
```
105+
95106
<!-- tabs:end -->
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pandas as pd
2+
3+
4+
def combine_two_tables(person: pd.DataFrame, address: pd.DataFrame) -> pd.DataFrame:
5+
return pd.merge(left=person, right=address, how="left", on="personId")[
6+
["firstName", "lastName", "city", "state"]
7+
]

‎solution/0100-0199/0176.Second Highest Salary/README.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,28 @@ WITH T AS (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rk FROM Em
113113
SELECT (SELECT DISTINCT salary FROM T WHERE rk = 2) AS SecondHighestSalary;
114114
```
115115

116+
```python
117+
import pandas as pd
118+
119+
120+
def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame:
121+
# Drop any duplicate salary values to avoid counting duplicates as separate salary ranks
122+
unique_salaries = employee["salary"].drop_duplicates()
123+
124+
# Sort the unique salaries in descending order and get the second highest salary
125+
second_highest = (
126+
unique_salaries.nlargest(2).iloc[-1] if len(unique_salaries) >= 2 else None
127+
)
128+
129+
# If the second highest salary doesn't exist (e.g., there are fewer than two unique salaries), return None
130+
if second_highest is None:
131+
return pd.DataFrame({"SecondHighestSalary": [None]})
132+
133+
# Create a DataFrame with the second highest salary
134+
result_df = pd.DataFrame({"SecondHighestSalary": [second_highest]})
135+
136+
return result_df
137+
138+
```
139+
116140
<!-- tabs:end -->

‎solution/0100-0199/0176.Second Highest Salary/README_EN.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,28 @@ WITH T AS (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rk FROM Em
9898
SELECT (SELECT DISTINCT salary FROM T WHERE rk = 2) AS SecondHighestSalary;
9999
```
100100

101+
```python
102+
import pandas as pd
103+
104+
105+
def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame:
106+
# Drop any duplicate salary values to avoid counting duplicates as separate salary ranks
107+
unique_salaries = employee["salary"].drop_duplicates()
108+
109+
# Sort the unique salaries in descending order and get the second highest salary
110+
second_highest = (
111+
unique_salaries.nlargest(2).iloc[-1] if len(unique_salaries) >= 2 else None
112+
)
113+
114+
# If the second highest salary doesn't exist (e.g., there are fewer than two unique salaries), return None
115+
if second_highest is None:
116+
return pd.DataFrame({"SecondHighestSalary": [None]})
117+
118+
# Create a DataFrame with the second highest salary
119+
result_df = pd.DataFrame({"SecondHighestSalary": [second_highest]})
120+
121+
return result_df
122+
123+
```
124+
101125
<!-- tabs:end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pandas as pd
2+
3+
4+
def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame:
5+
# Drop any duplicate salary values to avoid counting duplicates as separate salary ranks
6+
unique_salaries = employee["salary"].drop_duplicates()
7+
8+
# Sort the unique salaries in descending order and get the second highest salary
9+
second_highest = (
10+
unique_salaries.nlargest(2).iloc[-1] if len(unique_salaries) >= 2 else None
11+
)
12+
13+
# If the second highest salary doesn't exist (e.g., there are fewer than two unique salaries), return None
14+
if second_highest is None:
15+
return pd.DataFrame({"SecondHighestSalary": [None]})
16+
17+
# Create a DataFrame with the second highest salary
18+
result_df = pd.DataFrame({"SecondHighestSalary": [second_highest]})
19+
20+
return result_df

0 commit comments

Comments
(0)

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