Skip to content

Commit 5f2a1b4

Browse files
authored
Added tasks 2884-2888
1 parent cff5c3c commit 5f2a1b4

File tree

10 files changed

+274
-0
lines changed

10 files changed

+274
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2884\. Modify Columns
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 intends to give its employees a pay rise.
15+
16+
Write a solution to **modify** the `salary` column by multiplying each salary by 2.
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+
| Jack | 19666 |
28+
| Piper | 74754 |
29+
| Mia | 62509 |
30+
| Ulysses | 54866 |
31+
+---------+--------+
32+
33+
**Output:**
34+
35+
+---------+--------+
36+
| name | salary |
37+
+---------+--------+
38+
| Jack | 39332 |
39+
| Piper | 149508 |
40+
| Mia | 125018 |
41+
| Ulysses | 109732 |
42+
+---------+--------+
43+
44+
**Explanation:** Every salary has been doubled.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# #Easy #2023_12_23_Time_401_ms_(96.35%)_Space_60.2_MB_(54.27%)
2+
3+
import pandas as pd
4+
5+
def modifySalaryColumn(employees: pd.DataFrame) -> pd.DataFrame:
6+
employees['salary'] = employees['salary'] * 2
7+
return employees
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2885\. Rename Columns
2+
3+
Easy
4+
5+
DataFrame `students`
6+
7+
+-------------+--------+
8+
| Column Name | Type |
9+
+-------------+--------+
10+
| id | int |
11+
| first | object |
12+
| last | object |
13+
| age | int |
14+
+-------------+--------+
15+
16+
Write a solution to rename the columns as follows:
17+
18+
* `id` to `student_id`
19+
* `first` to `first_name`
20+
* `last` to `last_name`
21+
* `age` to `age_in_years`
22+
23+
The result format is in the following example.
24+
25+
**Example 1:** **Input:**
26+
27+
+----+---------+----------+-----+
28+
| id | first | last | age |
29+
+----+---------+----------+-----+
30+
| 1 | Mason | King | 6 |
31+
| 2 | Ava | Wright | 7 |
32+
| 3 | Taylor | Hall | 16 |
33+
| 4 | Georgia | Thompson | 18 |
34+
| 5 | Thomas | Moore | 10 |
35+
+----+---------+----------+-----+
36+
37+
**Output:**
38+
39+
+------------+------------+-----------+--------------+
40+
| student_id | first_name | last_name | age_in_years |
41+
+------------+------------+-----------+--------------+
42+
| 1 | Mason | King | 6 |
43+
| 2 | Ava | Wright | 7 |
44+
| 3 | Taylor | Hall | 16 |
45+
| 4 | Georgia | Thompson | 18 |
46+
| 5 | Thomas | Moore | 10 |
47+
+------------+------------+-----------+--------------+
48+
49+
**Explanation:** The column names are changed accordingly.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# #Easy #2023_12_23_Time_467_ms_(68.13%)_Space_60.7_MB_(15.08%)
2+
3+
import pandas as pd
4+
5+
def renameColumns(students: pd.DataFrame) -> pd.DataFrame:
6+
students.rename(columns={'id': 'student_id', 'first': 'first_name', 'last': 'last_name', 'age': 'age_in_years'}, inplace=True)
7+
return students
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2886\. Change Data Type
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+
| grade | float |
14+
+-------------+--------+
15+
16+
Write a solution to correct the errors:
17+
18+
The `grade` column is stored as floats, convert it to integers.
19+
20+
The result format is in the following example.
21+
22+
**Example 1:** **Input:** DataFrame students:
23+
24+
+------------+------+-----+-------+
25+
| student_id | name | age | grade |
26+
+------------+------+-----+-------+
27+
| 1 | Ava | 6 | 73.0 |
28+
| 2 | Kate | 15 | 87.0 |
29+
+------------+------+-----+-------+
30+
31+
**Output:**
32+
33+
+------------+------+-----+-------+
34+
| student_id | name | age | grade |
35+
+------------+------+-----+-------+
36+
| 1 | Ava | 6 | 73 |
37+
| 2 | Kate | 15 | 87 |
38+
+------------+------+-----+-------+
39+
40+
**Explanation:** The data types of the column grade is converted to int.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# #Easy #2023_12_23_Time_421_ms_(94.57%)_Space_59.2_MB_(92.43%)
2+
3+
import pandas as pd
4+
5+
def changeDatatype(students: pd.DataFrame) -> pd.DataFrame:
6+
students['grade'] = students['grade'].astype(int)
7+
return students
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2887\. Fill Missing Data
2+
3+
Easy
4+
5+
DataFrame `products`
6+
7+
+-------------+--------+
8+
| Column Name | Type |
9+
+-------------+--------+
10+
| name | object |
11+
| quantity | int |
12+
| price | int |
13+
+-------------+--------+
14+
15+
Write a solution to fill in the missing value as <code>**0**</code> in the `quantity` column.
16+
17+
The result format is in the following example.
18+
19+
**Example 1:**
20+
21+
**Input:**
22+
23+
+-----------------+----------+-------+
24+
| name | quantity | price |
25+
+-----------------+----------+-------+
26+
| Wristwatch | None | 135 |
27+
| WirelessEarbuds | None | 821 |
28+
| GolfClubs | 779 | 9319 |
29+
| Printer | 849 | 3051 |
30+
+-----------------+----------+-------+
31+
32+
**Output:**
33+
34+
+-----------------+----------+-------+
35+
| name | quantity | price |
36+
+-----------------+----------+-------+
37+
| Wristwatch | 0 | 135 |
38+
| WirelessEarbuds | 0 | 821 |
39+
| GolfClubs | 779 | 9319 |
40+
| Printer | 849 | 3051 |
41+
+-----------------+----------+-------+
42+
43+
**Explanation:** The quantity for Wristwatch and WirelessEarbuds are filled by 0.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# #Easy #2023_12_23_Time_404_ms_(97.11%)_Space_59.7_MB_(74.95%)
2+
3+
import pandas as pd
4+
5+
def fillMissingValues(products: pd.DataFrame) -> pd.DataFrame:
6+
products['quantity'].fillna(0, inplace=True)
7+
return products
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2888\. Reshape Data: Concatenate
2+
3+
Easy
4+
5+
DataFrame `df1`
6+
7+
+-------------+--------+
8+
| Column Name | Type |
9+
+-------------+--------+
10+
| student_id | int |
11+
| name | object |
12+
| age | int |
13+
+-------------+--------+
14+
15+
DataFrame `df2`
16+
17+
+-------------+--------+
18+
| Column Name | Type |
19+
+-------------+--------+
20+
| student_id | int |
21+
| name | object |
22+
| age | int |
23+
+-------------+--------+
24+
25+
Write a solution to concatenate these two DataFrames **vertically** into one DataFrame.
26+
27+
The result format is in the following example.
28+
29+
**Example 1:**
30+
31+
**Input: df1**
32+
33+
+------------+---------+-----+
34+
| student_id | name | age |
35+
+------------+---------+-----+
36+
| 1 | Mason | 8 |
37+
| 2 | Ava | 6 |
38+
| 3 | Taylor | 15 |
39+
| 4 | Georgia | 17 |
40+
+------------+---------+-----+
41+
42+
**df2**
43+
44+
+------------+------+-----+
45+
| student_id | name | age |
46+
+------------+------+-----+
47+
| 5 | Leo | 7 |
48+
| 6 | Alex | 7 |
49+
+------------+------+-----+
50+
51+
**Output:**
52+
53+
+------------+---------+-----+
54+
| student_id | name | age |
55+
+------------+---------+-----+
56+
| 1 | Mason | 8 |
57+
| 2 | Ava | 6 |
58+
| 3 | Taylor | 15 |
59+
| 4 | Georgia | 17 |
60+
| 5 | Leo | 7 |
61+
| 6 | Alex | 7 |
62+
+------------+---------+-----+
63+
64+
**Explanation:** The two DataFramess are stacked vertically, and their rows are combined.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# #Easy #2023_12_23_Time_441_ms_(96.26%)_Space_59_MB_(97.37%)
2+
3+
import pandas as pd
4+
5+
def concatenateTables(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame:
6+
return pd.concat([df1, df2], ignore_index=True)

0 commit comments

Comments
 (0)