Skip to content

Commit 1b24aae

Browse files
committed
Add pandas code 607,619,620,627
1 parent 8aa17e9 commit 1b24aae

4 files changed

+77
-0
lines changed

Diff for: 607.sales-person.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# https://leetcode.cn/problems/sales-person
2+
3+
import pandas as pd
4+
5+
6+
def sales_person(sales_person: pd.DataFrame, company: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame:
7+
'''
8+
Date: 2023.08.10
9+
Pass/Error/Bug: 1/0/0
10+
执行用时: 528 ms, 在所有 Python3 提交中击败了 12.58% 的用户
11+
内存消耗:61.80 Mb, 在所有 Python3 提交中击败了 5.60% 的用户
12+
'''
13+
names = []
14+
15+
for g, gdf in pd.merge(
16+
sales_person,
17+
pd.merge(orders, company, on='com_id', how='outer'),
18+
on='sales_id', how='left'
19+
).groupby('name_x'):
20+
if 'RED' in gdf['name_y'].values:
21+
pass
22+
else:
23+
names.append(g)
24+
25+
return pd.DataFrame({'name': names})

Diff for: 619.biggest-single-number.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# https://leetcode.cn/problems/biggest-single-number
2+
3+
import pandas as pd
4+
5+
6+
def biggest_single_number(my_numbers: pd.DataFrame) -> pd.DataFrame:
7+
'''
8+
Date: 2023.08.31
9+
Pass/Error/Bug: 1/0/0
10+
执行用时: 280 ms, 在所有 Python3 提交中击败了 53.34% 的用户
11+
内存消耗:59.30 Mb, 在所有 Python3 提交中击败了 61.97% 的用户
12+
'''
13+
rdf = (
14+
my_numbers
15+
.groupby('num').size()
16+
.reset_index()
17+
.rename(columns={0:'count'})
18+
.query('count==1')
19+
.sort_values('num', ascending=False)
20+
.head(1)
21+
.drop('count', axis=1)
22+
)
23+
if rdf.shape[0] == 0:
24+
return pd.DataFrame([None], columns=['num'])
25+
else:
26+
return rdf

Diff for: 620.not-boring-movies.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# https://leetcode.cn/problems/not-boring-movies
2+
3+
import pandas as pd
4+
5+
6+
def not_boring_movies(cinema: pd.DataFrame) -> pd.DataFrame:
7+
'''
8+
Date: 2023.09.01
9+
Pass/Error/Bug: 1/0/0
10+
执行用时: 256 ms, 在所有 Python3 提交中击败了 60.00% 的用户
11+
内存消耗:60.20 Mb, 在所有 Python3 提交中击败了 5.45% 的用户
12+
'''
13+
return cinema.query('description != "boring" and id % 2 == 1').sort_values('rating', ascending=False)

Diff for: 627.swap-salary.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# https://leetcode.cn/problems/swap-salary
2+
3+
import pandas as pd
4+
5+
6+
def swap_salary(salary: pd.DataFrame) -> pd.DataFrame:
7+
'''
8+
Date: 2023.09.02
9+
Pass/Error/Bug: 1/0/0
10+
执行用时: 236 ms, 在所有 Python3 提交中击败了 80.85% 的用户
11+
内存消耗:57.19 Mb, 在所有 Python3 提交中击败了 93.62% 的用户
12+
'''
13+
return salary.replace({'sex':{'f':'m', 'm':'f'}})

0 commit comments

Comments
 (0)