Movatterモバイル変換


[0]ホーム

URL:


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

Commitcff5c3c

Browse files
authored
Added tasks 2879-2883
1 parentfd1cee4 commitcff5c3c

File tree

10 files changed

+257
-0
lines changed

10 files changed

+257
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2879\. Display the First Three Rows
2+
3+
Easy
4+
5+
DataFrame:`employees`
6+
7+
+-------------+--------+
8+
| Column Name | Type |
9+
+-------------+--------+
10+
| employee_id | int |
11+
| name | object |
12+
| department | object |
13+
| salary | int |
14+
+-------------+--------+
15+
16+
Write a solution to display the**first`3`** rows of this DataFrame.
17+
18+
**Example 1:**
19+
20+
**Input:** DataFrame employees
21+
22+
+-------------+-----------+-----------------------+--------+
23+
| employee_id | name | department | salary |
24+
+-------------+-----------+-----------------------+--------+
25+
| 3 | Bob | Operations | 48675 |
26+
| 90 | Alice | Sales | 11096 |
27+
| 9 | Tatiana | Engineering | 33805 |
28+
| 60 | Annabelle | InformationTechnology | 37678 |
29+
| 49 | Jonathan | HumanResources | 23793 |
30+
| 43 | Khaled | Administration | 40454 |
31+
+-------------+-----------+-----------------------+--------+
32+
33+
**Output:**
34+
35+
+-------------+---------+-------------+--------+
36+
| employee_id | name | department | salary |
37+
+-------------+---------+-------------+--------+
38+
| 3 | Bob | Operations | 48675 |
39+
| 90 | Alice | Sales | 11096 |
40+
| 9 | Tatiana | Engineering | 33805 |
41+
+-------------+---------+-------------+--------+
42+
43+
**Explanation:** Only the first 3 rows are displayed.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# #Easy #2023_12_23_Time_406_ms_(96.44%)_Space_60.8_MB_(5.67%)
2+
3+
importpandasaspd
4+
5+
defselectFirstRows(zs:pd.DataFrame)->pd.DataFrame:
6+
returnzs.head(3)
7+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2880\. Select Data
2+
3+
Easy
4+
5+
DataFrame students
6+
7+
+-------------+--------+
8+
| Column Name | Type |
9+
+-------------+--------+
10+
| student_id | int |
11+
| name | object |
12+
| age | int |
13+
+-------------+--------+
14+
15+
Write a solution to select the name and age of the student with`student_id = 101`.
16+
17+
The result format is in the following example.
18+
19+
**Example 1: Input:**
20+
21+
+------------+---------+-----+
22+
| student_id | name | age |
23+
+------------+---------+-----+
24+
| 101 | Ulysses | 13 |
25+
| 53 | William | 10 |
26+
| 128 | Henry | 6 |
27+
| 3 | Henry | 11 |
28+
+------------+---------+-----+
29+
30+
**Output:**
31+
32+
+---------+-----+
33+
| name | age |
34+
+---------+-----+
35+
| Ulysses | 13 |
36+
+---------+-----+
37+
38+
**Explanation:** Student Ulysses has student_id = 101, we select the name and age.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# #Easy #2023_12_23_Time_428_ms_(94.99%)_Space_60_MB_(83.82%)
2+
3+
importpandasaspd
4+
5+
defselectData(students:pd.DataFrame)->pd.DataFrame:
6+
returnstudents[students.student_id==101][['name','age']]
7+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2881\. Create a New Column
2+
3+
Easy
4+
5+
DataFrame`employees`
6+
7+
+-------------+--------+
8+
| Column Name | Type. |
9+
+-------------+--------+
10+
| name | object |
11+
| salary | int. |
12+
+-------------+--------+
13+
14+
A company plans to provide its employees with a bonus.
15+
16+
Write a solution to create a new column name`bonus` that contains the**doubled values** of the`salary` column.
17+
18+
The result format is in the following example.
19+
20+
**Example 1:**
21+
22+
**Input:** DataFrame employees
23+
24+
+---------+--------+
25+
| name | salary |
26+
+---------+--------+
27+
| Piper | 4548 |
28+
| Grace | 28150 |
29+
| Georgia | 1103 |
30+
| Willow | 6593 |
31+
| Finn | 74576 |
32+
| Thomas | 24433 |
33+
+---------+--------+
34+
35+
**Output:**
36+
37+
+---------+--------+--------+
38+
| name | salary | bonus |
39+
+---------+--------+--------+
40+
| Piper | 4548 | 9096 |
41+
| Grace | 28150 | 56300 |
42+
| Georgia | 1103 | 2206 |
43+
| Willow | 6593 | 13186 |
44+
| Finn | 74576 | 149152 |
45+
| Thomas | 24433 | 48866 |
46+
+---------+--------+--------+
47+
48+
**Explanation:** A new column bonus is created by doubling the value in the column salary.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# #Easy #2023_12_23_Time_437_ms_(81.98%)_Space_60.3_MB_(37.80%)
2+
3+
importpandasaspd
4+
5+
defcreateBonusColumn(employees:pd.DataFrame)->pd.DataFrame:
6+
employees["bonus"]=employees["salary"]*2
7+
returnemployees
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2882\. Drop Duplicate Rows
2+
3+
Easy
4+
5+
DataFrame customers
6+
7+
+-------------+--------+
8+
| Column Name | Type |
9+
+-------------+--------+
10+
| customer_id | int |
11+
| name | object |
12+
| email | object |
13+
+-------------+--------+
14+
15+
There are some duplicate rows in the DataFrame based on the`email` column.
16+
17+
Write a solution to remove these duplicate rows and keep only the**first** occurrence.
18+
19+
The result format is in the following example.
20+
21+
**Example 1:**
22+
23+
**Input:**
24+
25+
+-------------+---------+---------------------+
26+
| customer_id | name | email |
27+
+-------------+---------+---------------------+
28+
| 1 | Ella | emily@example.com |
29+
| 2 | David | michael@example.com |
30+
| 3 | Zachary | sarah@example.com |
31+
| 4 | Alice | john@example.com |
32+
| 5 | Finn | john@example.com |
33+
| 6 | Violet | alice@example.com |
34+
+-------------+---------+---------------------+
35+
36+
**Output:**
37+
38+
+-------------+---------+---------------------+
39+
| customer_id | name | email |
40+
+-------------+---------+---------------------+
41+
| 1 | Ella | emily@example.com |
42+
| 2 | David | michael@example.com |
43+
| 3 | Zachary | sarah@example.com |
44+
| 4 | Alice | john@example.com |
45+
| 6 | Violet | alice@example.com |
46+
+-------------+---------+---------------------+
47+
48+
**Explanation:** Alic (customer_id = 4) and Finn (customer_id = 5) both usejohn@example.com, so only the first occurrence of this email is retained.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# #Easy #2023_12_23_Time_405_ms_(97.36%)_Space_60.2_MB_(75.25%)
2+
3+
importpandasaspd
4+
5+
defdropDuplicateEmails(customers:pd.DataFrame)->pd.DataFrame:
6+
customers.drop_duplicates(subset='email',keep='first',inplace=True)
7+
returncustomers
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2883\. Drop Missing Data
2+
3+
Easy
4+
5+
DataFrame students
6+
7+
+-------------+--------+
8+
| Column Name | Type |
9+
+-------------+--------+
10+
| student_id | int |
11+
| name | object |
12+
| age | int |
13+
+-------------+--------+
14+
15+
There are some rows having missing values in the`name` column.
16+
17+
Write a solution to remove the rows with missing values.
18+
19+
The result format is in the following example.
20+
21+
**Example 1:**
22+
23+
**Input:**
24+
25+
+------------+---------+-----+
26+
| student_id | name | age |
27+
+------------+---------+-----+
28+
| 32 | Piper | 5 |
29+
| 217 | None | 19 |
30+
| 779 | Georgia | 20 |
31+
| 849 | Willow | 14 |
32+
+------------+---------+-----+
33+
34+
**Output:**
35+
36+
+------------+---------+-----+
37+
| student_id | name | age |
38+
+------------+---------+-----+
39+
| 32 | Piper | 5 |
40+
| 779 | Georgia | 20 |
41+
| 849 | Willow | 14 |
42+
+------------+---------+-----+
43+
44+
**Explanation:** Student with id 217 havs empty value in the name column, so it will be removed.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# #Easy #2023_12_23_Time_429_ms_(94.97%)_Space_61.2_MB_(12.30%)
2+
3+
importpandasaspd
4+
5+
defdropMissingData(students:pd.DataFrame)->pd.DataFrame:
6+
r=pd.DataFrame(students)
7+
r.dropna(subset='name',inplace=True)
8+
returnr

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp