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 6a9a219

Browse files
Add new files
1 parent c30fe4c commit 6a9a219

File tree

5 files changed

+238
-0
lines changed

5 files changed

+238
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
2877. Create a DataFrame from List
3+
Solved
4+
Easy
5+
Companies
6+
Hint
7+
Write a solution to create a DataFrame from a 2D list called student_data. This 2D list contains the IDs and ages of some students.
8+
9+
The DataFrame should have two columns, student_id and age, and be in the same order as the original 2D list.
10+
11+
The result format is in the following example.
12+
13+
Example 1:
14+
15+
Input:
16+
student_data:
17+
[
18+
[1, 15],
19+
[2, 11],
20+
[3, 11],
21+
[4, 20]
22+
]
23+
Output:
24+
+------------+-----+
25+
| student_id | age |
26+
+------------+-----+
27+
| 1 | 15 |
28+
| 2 | 11 |
29+
| 3 | 11 |
30+
| 4 | 20 |
31+
+------------+-----+
32+
Explanation:
33+
A DataFrame was created on top of student_data, with two columns named student_id and age."
34+
"""
35+
36+
import pandas as pd
37+
38+
def createDataframe(student_data: List[List[int]]) -> pd.DataFrame:
39+
return pd.DataFrame(student_data, columns=["student_id", "age"])
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
2878. Get the Size of a DataFrame
3+
Solved
4+
Easy
5+
Companies
6+
Hint
7+
DataFrame players:
8+
+-------------+--------+
9+
| Column Name | Type |
10+
+-------------+--------+
11+
| player_id | int |
12+
| name | object |
13+
| age | int |
14+
| position | object |
15+
| ... | ... |
16+
+-------------+--------+
17+
Write a solution to calculate and display the number of rows and columns of players.
18+
19+
Return the result as an array:
20+
21+
[number of rows, number of columns]
22+
23+
The result format is in the following example.
24+
25+
Example 1:
26+
27+
Input:
28+
+-----------+----------+-----+-------------+--------------------+
29+
| player_id | name | age | position | team |
30+
+-----------+----------+-----+-------------+--------------------+
31+
| 846 | Mason | 21 | Forward | RealMadrid |
32+
| 749 | Riley | 30 | Winger | Barcelona |
33+
| 155 | Bob | 28 | Striker | ManchesterUnited |
34+
| 583 | Isabella | 32 | Goalkeeper | Liverpool |
35+
| 388 | Zachary | 24 | Midfielder | BayernMunich |
36+
| 883 | Ava | 23 | Defender | Chelsea |
37+
| 355 | Violet | 18 | Striker | Juventus |
38+
| 247 | Thomas | 27 | Striker | ParisSaint-Germain |
39+
| 761 | Jack | 33 | Midfielder | ManchesterCity |
40+
| 642 | Charlie | 36 | Center-back | Arsenal |
41+
+-----------+----------+-----+-------------+--------------------+
42+
Output:
43+
[10, 5]
44+
Explanation:
45+
This DataFrame contains 10 rows and 5 columns."
46+
"""
47+
48+
import pandas as pd
49+
50+
def getDataframeSize(players: pd.DataFrame) -> List[int]:
51+
return list(players.shape)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
2880. Select Data
3+
Solved
4+
Easy
5+
Companies
6+
Hint
7+
DataFrame students
8+
+-------------+--------+
9+
| Column Name | Type |
10+
+-------------+--------+
11+
| student_id | int |
12+
| name | object |
13+
| age | int |
14+
+-------------+--------+
15+
16+
Write a solution to select the name and age of the student with student_id = 101.
17+
18+
The result format is in the following example.
19+
20+
Example 1:
21+
Input:
22+
+------------+---------+-----+
23+
| student_id | name | age |
24+
+------------+---------+-----+
25+
| 101 | Ulysses | 13 |
26+
| 53 | William | 10 |
27+
| 128 | Henry | 6 |
28+
| 3 | Henry | 11 |
29+
+------------+---------+-----+
30+
Output:
31+
+---------+-----+
32+
| name | age |
33+
+---------+-----+
34+
| Ulysses | 13 |
35+
+---------+-----+
36+
Explanation:
37+
Student Ulysses has student_id = 101, we select the name and age."
38+
"""
39+
40+
import pandas as pd
41+
42+
def selectData(students: pd.DataFrame) -> pd.DataFrame:
43+
return students.loc[students["student_id"] == 101, ["name", "age"]]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
2881. Create a New Column
3+
Solved
4+
Easy
5+
Companies
6+
Hint
7+
DataFrame employees
8+
+-------------+--------+
9+
| Column Name | Type. |
10+
+-------------+--------+
11+
| name | object |
12+
| salary | int. |
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:
23+
DataFrame employees
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+
Output:
35+
+---------+--------+--------+
36+
| name | salary | bonus |
37+
+---------+--------+--------+
38+
| Piper | 4548 | 9096 |
39+
| Grace | 28150 | 56300 |
40+
| Georgia | 1103 | 2206 |
41+
| Willow | 6593 | 13186 |
42+
| Finn | 74576 | 149152 |
43+
| Thomas | 24433 | 48866 |
44+
+---------+--------+--------+
45+
Explanation:
46+
A new column bonus is created by doubling the value in the column salary."
47+
"""
48+
49+
import pandas as pd
50+
51+
def createBonusColumn(employees: pd.DataFrame) -> pd.DataFrame:
52+
employees["bonus"] = employees["salary"] * 2
53+
return employees
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
2882. Drop Duplicate Rows
3+
Solved
4+
Easy
5+
Companies
6+
Hint
7+
DataFrame customers
8+
+-------------+--------+
9+
| Column Name | Type |
10+
+-------------+--------+
11+
| customer_id | int |
12+
| name | object |
13+
| email | object |
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+
22+
23+
Example 1:
24+
Input:
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+
Output:
36+
+-------------+---------+---------------------+
37+
| customer_id | name | email |
38+
+-------------+---------+---------------------+
39+
| 1 | Ella | emily@example.com |
40+
| 2 | David | michael@example.com |
41+
| 3 | Zachary | sarah@example.com |
42+
| 4 | Alice | john@example.com |
43+
| 6 | Violet | alice@example.com |
44+
+-------------+---------+---------------------+
45+
Explanation:
46+
Alic (customer_id = 4) and Finn (customer_id = 5) both use john@example.com, so only the first occurrence of this email is retained."
47+
"""
48+
49+
import pandas as pd
50+
51+
def dropDuplicateEmails(customers: pd.DataFrame) -> pd.DataFrame:
52+
return customers.drop_duplicates("email")

0 commit comments

Comments
(0)

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