forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
2884\. Modify Columns | ||
|
||
Easy | ||
|
||
DataFrame `employees` | ||
|
||
+-------------+--------+ | ||
| Column Name | Type | | ||
+-------------+--------+ | ||
| name | object | | ||
| salary | int | | ||
+-------------+--------+ | ||
|
||
A company intends to give its employees a pay rise. | ||
|
||
Write a solution to **modify** the `salary` column by multiplying each salary by 2. | ||
|
||
The result format is in the following example. | ||
|
||
**Example 1:** | ||
|
||
**Input:** DataFrame employees | ||
|
||
+---------+--------+ | ||
| name | salary | | ||
+---------+--------+ | ||
| Jack | 19666 | | ||
| Piper | 74754 | | ||
| Mia | 62509 | | ||
| Ulysses | 54866 | | ||
+---------+--------+ | ||
|
||
**Output:** | ||
|
||
+---------+--------+ | ||
| name | salary | | ||
+---------+--------+ | ||
| Jack | 39332 | | ||
| Piper | 149508 | | ||
| Mia | 125018 | | ||
| Ulysses | 109732 | | ||
+---------+--------+ | ||
|
||
**Explanation:** Every salary has been doubled. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# #Easy #2023_12_23_Time_401_ms_(96.35%)_Space_60.2_MB_(54.27%) | ||
|
||
import pandas as pd | ||
|
||
def modifySalaryColumn(employees: pd.DataFrame) -> pd.DataFrame: | ||
employees['salary'] = employees['salary'] * 2 | ||
return employees |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
2885\. Rename Columns | ||
|
||
Easy | ||
|
||
DataFrame `students` | ||
|
||
+-------------+--------+ | ||
| Column Name | Type | | ||
+-------------+--------+ | ||
| id | int | | ||
| first | object | | ||
| last | object | | ||
| age | int | | ||
+-------------+--------+ | ||
|
||
Write a solution to rename the columns as follows: | ||
|
||
* `id` to `student_id` | ||
* `first` to `first_name` | ||
* `last` to `last_name` | ||
* `age` to `age_in_years` | ||
|
||
The result format is in the following example. | ||
|
||
**Example 1:** **Input:** | ||
|
||
+----+---------+----------+-----+ | ||
| id | first | last | age | | ||
+----+---------+----------+-----+ | ||
| 1 | Mason | King | 6 | | ||
| 2 | Ava | Wright | 7 | | ||
| 3 | Taylor | Hall | 16 | | ||
| 4 | Georgia | Thompson | 18 | | ||
| 5 | Thomas | Moore | 10 | | ||
+----+---------+----------+-----+ | ||
|
||
**Output:** | ||
|
||
+------------+------------+-----------+--------------+ | ||
| student_id | first_name | last_name | age_in_years | | ||
+------------+------------+-----------+--------------+ | ||
| 1 | Mason | King | 6 | | ||
| 2 | Ava | Wright | 7 | | ||
| 3 | Taylor | Hall | 16 | | ||
| 4 | Georgia | Thompson | 18 | | ||
| 5 | Thomas | Moore | 10 | | ||
+------------+------------+-----------+--------------+ | ||
|
||
**Explanation:** The column names are changed accordingly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# #Easy #2023_12_23_Time_467_ms_(68.13%)_Space_60.7_MB_(15.08%) | ||
|
||
import pandas as pd | ||
|
||
def renameColumns(students: pd.DataFrame) -> pd.DataFrame: | ||
students.rename(columns={'id': 'student_id', 'first': 'first_name', 'last': 'last_name', 'age': 'age_in_years'}, inplace=True) | ||
return students |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
2886\. Change Data Type | ||
|
||
Easy | ||
|
||
DataFrame `students` | ||
|
||
+-------------+--------+ | ||
| Column Name | Type | | ||
+-------------+--------+ | ||
| student_id | int | | ||
| name | object | | ||
| age | int | | ||
| grade | float | | ||
+-------------+--------+ | ||
|
||
Write a solution to correct the errors: | ||
|
||
The `grade` column is stored as floats, convert it to integers. | ||
|
||
The result format is in the following example. | ||
|
||
**Example 1:** **Input:** DataFrame students: | ||
|
||
+------------+------+-----+-------+ | ||
| student_id | name | age | grade | | ||
+------------+------+-----+-------+ | ||
| 1 | Ava | 6 | 73.0 | | ||
| 2 | Kate | 15 | 87.0 | | ||
+------------+------+-----+-------+ | ||
|
||
**Output:** | ||
|
||
+------------+------+-----+-------+ | ||
| student_id | name | age | grade | | ||
+------------+------+-----+-------+ | ||
| 1 | Ava | 6 | 73 | | ||
| 2 | Kate | 15 | 87 | | ||
+------------+------+-----+-------+ | ||
|
||
**Explanation:** The data types of the column grade is converted to int. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# #Easy #2023_12_23_Time_421_ms_(94.57%)_Space_59.2_MB_(92.43%) | ||
|
||
import pandas as pd | ||
|
||
def changeDatatype(students: pd.DataFrame) -> pd.DataFrame: | ||
students['grade'] = students['grade'].astype(int) | ||
return students |
43 changes: 43 additions & 0 deletions
43
src/main/java/g2801_2900/s2887_fill_missing_data/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
2887\. Fill Missing Data | ||
|
||
Easy | ||
|
||
DataFrame `products` | ||
|
||
+-------------+--------+ | ||
| Column Name | Type | | ||
+-------------+--------+ | ||
| name | object | | ||
| quantity | int | | ||
| price | int | | ||
+-------------+--------+ | ||
|
||
Write a solution to fill in the missing value as <code>**0**</code> in the `quantity` column. | ||
|
||
The result format is in the following example. | ||
|
||
**Example 1:** | ||
|
||
**Input:** | ||
|
||
+-----------------+----------+-------+ | ||
| name | quantity | price | | ||
+-----------------+----------+-------+ | ||
| Wristwatch | None | 135 | | ||
| WirelessEarbuds | None | 821 | | ||
| GolfClubs | 779 | 9319 | | ||
| Printer | 849 | 3051 | | ||
+-----------------+----------+-------+ | ||
|
||
**Output:** | ||
|
||
+-----------------+----------+-------+ | ||
| name | quantity | price | | ||
+-----------------+----------+-------+ | ||
| Wristwatch | 0 | 135 | | ||
| WirelessEarbuds | 0 | 821 | | ||
| GolfClubs | 779 | 9319 | | ||
| Printer | 849 | 3051 | | ||
+-----------------+----------+-------+ | ||
|
||
**Explanation:** The quantity for Wristwatch and WirelessEarbuds are filled by 0. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# #Easy #2023_12_23_Time_404_ms_(97.11%)_Space_59.7_MB_(74.95%) | ||
|
||
import pandas as pd | ||
|
||
def fillMissingValues(products: pd.DataFrame) -> pd.DataFrame: | ||
products['quantity'].fillna(0, inplace=True) | ||
return products |
64 changes: 64 additions & 0 deletions
64
src/main/java/g2801_2900/s2888_reshape_data_concatenate/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
2888\. Reshape Data: Concatenate | ||
|
||
Easy | ||
|
||
DataFrame `df1` | ||
|
||
+-------------+--------+ | ||
| Column Name | Type | | ||
+-------------+--------+ | ||
| student_id | int | | ||
| name | object | | ||
| age | int | | ||
+-------------+--------+ | ||
|
||
DataFrame `df2` | ||
|
||
+-------------+--------+ | ||
| Column Name | Type | | ||
+-------------+--------+ | ||
| student_id | int | | ||
| name | object | | ||
| age | int | | ||
+-------------+--------+ | ||
|
||
Write a solution to concatenate these two DataFrames **vertically** into one DataFrame. | ||
|
||
The result format is in the following example. | ||
|
||
**Example 1:** | ||
|
||
**Input: df1** | ||
|
||
+------------+---------+-----+ | ||
| student_id | name | age | | ||
+------------+---------+-----+ | ||
| 1 | Mason | 8 | | ||
| 2 | Ava | 6 | | ||
| 3 | Taylor | 15 | | ||
| 4 | Georgia | 17 | | ||
+------------+---------+-----+ | ||
|
||
**df2** | ||
|
||
+------------+------+-----+ | ||
| student_id | name | age | | ||
+------------+------+-----+ | ||
| 5 | Leo | 7 | | ||
| 6 | Alex | 7 | | ||
+------------+------+-----+ | ||
|
||
**Output:** | ||
|
||
+------------+---------+-----+ | ||
| student_id | name | age | | ||
+------------+---------+-----+ | ||
| 1 | Mason | 8 | | ||
| 2 | Ava | 6 | | ||
| 3 | Taylor | 15 | | ||
| 4 | Georgia | 17 | | ||
| 5 | Leo | 7 | | ||
| 6 | Alex | 7 | | ||
+------------+---------+-----+ | ||
|
||
**Explanation:** The two DataFramess are stacked vertically, and their rows are combined. |
6 changes: 6 additions & 0 deletions
6
src/main/java/g2801_2900/s2888_reshape_data_concatenate/solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# #Easy #2023_12_23_Time_441_ms_(96.26%)_Space_59_MB_(97.37%) | ||
|
||
import pandas as pd | ||
|
||
def concatenateTables(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame: | ||
return pd.concat([df1, df2], ignore_index=True) |