Skip to content

Commit 17cedee

Browse files
authored
feat: add pandas solutions to lc problems: No.2877~2891 (doocs#1750)
* No.2877.Create a DataFrame from List * No.2878.Get the Size of a DataFrame * No.2879.Display the First Three Rows * No.2880.Select Data * No.2881.Create a New Column * No.2882.Drop Duplicate Rows * No.2883.Drop Missing Data * No.2884.Modify Columns * No.2885.Rename Columns * No.2886.Change Data Type * No.2887.Fill Missing Data * No.2888.Reshape Data Concatenate * No.2889.Reshape Data Pivot * No.2890.Reshape Data Melt * No.2891.Method Chaining
1 parent 99c4667 commit 17cedee

File tree

45 files changed

+222
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+222
-20
lines changed

solution/2800-2899/2877.Create a DataFrame from List/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ A DataFrame was created on top of student_data, with two columns named <code>stu
4848
<!-- 这里可写当前语言的特殊实现逻辑 -->
4949

5050
```python
51+
import pandas as pd
5152

53+
54+
def createDataframe(student_data: List[List[int]]) -> pd.DataFrame:
55+
return pd.DataFrame(student_data, columns=['student_id', 'age'])
5256
```
5357

5458
### **...**

solution/2800-2899/2877.Create a DataFrame from List/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ A DataFrame was created on top of student_data, with two columns named <code>stu
4242
### **Pandas**
4343

4444
```python
45+
import pandas as pd
4546

47+
48+
def createDataframe(student_data: List[List[int]]) -> pd.DataFrame:
49+
return pd.DataFrame(student_data, columns=['student_id', 'age'])
4650
```
4751

4852
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import pandas as pd
2+
3+
4+
def createDataframe(student_data: List[List[int]]) -> pd.DataFrame:
5+
return pd.DataFrame(student_data, columns=['student_id', 'age'])

solution/2800-2899/2878.Get the Size of a DataFrame/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ This DataFrame contains 10 rows and 5 columns.
6363
<!-- 这里可写当前语言的特殊实现逻辑 -->
6464

6565
```python
66+
import pandas as pd
6667

68+
69+
def getDataframeSize(players: pd.DataFrame) -> List[int]:
70+
return list(players.shape)
6771
```
6872

6973
### **...**

solution/2800-2899/2878.Get the Size of a DataFrame/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ This DataFrame contains 10 rows and 5 columns.
5757
### **Pandas**
5858

5959
```python
60+
import pandas as pd
6061

62+
63+
def getDataframeSize(players: pd.DataFrame) -> List[int]:
64+
return list(players.shape)
6165
```
6266

6367
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import pandas as pd
2+
3+
4+
def getDataframeSize(players: pd.DataFrame) -> List[int]:
5+
return list(players.shape)

solution/2800-2899/2879.Display the First Three Rows/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ Only the first 3 rows are displayed.</pre>
5858
<!-- 这里可写当前语言的特殊实现逻辑 -->
5959

6060
```python
61+
import pandas as pd
6162

63+
64+
def selectFirstRows(employees: pd.DataFrame) -> pd.DataFrame:
65+
return employees.head(3)
6266
```
6367

6468
### **...**

solution/2800-2899/2879.Display the First Three Rows/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ Only the first 3 rows are displayed.</pre>
5252
### **Pandas**
5353

5454
```python
55+
import pandas as pd
5556

57+
58+
def selectFirstRows(employees: pd.DataFrame) -> pd.DataFrame:
59+
return employees.head(3)
5660
```
5761

5862
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import pandas as pd
2+
3+
4+
def selectFirstRows(employees: pd.DataFrame) -> pd.DataFrame:
5+
return employees.head(3)

solution/2800-2899/2880.Select Data/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ Input:</strong>
5454
<!-- 这里可写当前语言的特殊实现逻辑 -->
5555

5656
```python
57+
import pandas as pd
5758

59+
60+
def selectData(students: pd.DataFrame) -> pd.DataFrame:
61+
return students[students['student_id'] == 101][['name', 'age']]
5862
```
5963

6064
### **Java**

solution/2800-2899/2880.Select Data/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ Input:</strong>
4848
### **Pandas**
4949

5050
```python
51+
import pandas as pd
5152

53+
54+
def selectData(students: pd.DataFrame) -> pd.DataFrame:
55+
return students[students['student_id'] == 101][['name', 'age']]
5256
```
5357

5458
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import pandas as pd
2+
3+
4+
def selectData(students: pd.DataFrame) -> pd.DataFrame:
5+
return students[students['student_id'] == 101][['name', 'age']]

solution/2800-2899/2881.Create a New Column/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ A new column bonus is created by doubling the value in the column salary.</pre>
6363
<!-- 这里可写当前语言的特殊实现逻辑 -->
6464

6565
```python
66+
import pandas as pd
6667

68+
69+
def createBonusColumn(employees: pd.DataFrame) -> pd.DataFrame:
70+
employees['bonus'] = employees['salary'] * 2
71+
return employees
6772
```
6873

6974
### **...**

solution/2800-2899/2881.Create a New Column/README_EN.md

+5
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ A new column bonus is created by doubling the value in the column salary.</pre>
5757
### **Pandas**
5858

5959
```python
60+
import pandas as pd
6061

62+
63+
def createBonusColumn(employees: pd.DataFrame) -> pd.DataFrame:
64+
employees['bonus'] = employees['salary'] * 2
65+
return employees
6166
```
6267

6368
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pandas as pd
2+
3+
4+
def createBonusColumn(employees: pd.DataFrame) -> pd.DataFrame:
5+
employees['bonus'] = employees['salary'] * 2
6+
return employees

solution/2800-2899/2882.Drop Duplicate Rows/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ Alic (customer_id = 4) and Finn (customer_id = 5) both use john@example.com, so
6262
<!-- 这里可写当前语言的特殊实现逻辑 -->
6363

6464
```python
65+
import pandas as pd
6566

67+
68+
def dropDuplicateEmails(customers: pd.DataFrame) -> pd.DataFrame:
69+
return customers.drop_duplicates(subset=['email'])
6670
```
6771

6872
### **...**

solution/2800-2899/2882.Drop Duplicate Rows/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ Alic (customer_id = 4) and Finn (customer_id = 5) both use john@example.com, so
5656
### **Pandas**
5757

5858
```python
59+
import pandas as pd
5960

61+
62+
def dropDuplicateEmails(customers: pd.DataFrame) -> pd.DataFrame:
63+
return customers.drop_duplicates(subset=['email'])
6064
```
6165

6266
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import pandas as pd
2+
3+
4+
def dropDuplicateEmails(customers: pd.DataFrame) -> pd.DataFrame:
5+
return customers.drop_duplicates(subset=['email'])

solution/2800-2899/2883.Drop Missing Data/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ Students with ids 779 and 849 have empty values in the name column, so they will
5757
<!-- 这里可写当前语言的特殊实现逻辑 -->
5858

5959
```python
60+
import pandas as pd
6061

62+
63+
def dropMissingData(students: pd.DataFrame) -> pd.DataFrame:
64+
return students[students['name'].notnull()]
6165
```
6266

6367
### **...**

solution/2800-2899/2883.Drop Missing Data/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ Students with ids 779 and 849 have empty values in the name column, so they will
5151
### **Pandas**
5252

5353
```python
54+
import pandas as pd
5455

56+
57+
def dropMissingData(students: pd.DataFrame) -> pd.DataFrame:
58+
return students[students['name'].notnull()]
5559
```
5660

5761
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import pandas as pd
2+
3+
4+
def dropMissingData(students: pd.DataFrame) -> pd.DataFrame:
5+
return students[students['name'].notnull()]

solution/2800-2899/2884.Modify Columns/README.md

+5-20
Original file line numberDiff line numberDiff line change
@@ -54,32 +54,17 @@ DataFrame <code>employees</code>
5454

5555
<!-- tabs:start -->
5656

57-
### **Python3**
57+
### **Pandas**
5858

5959
<!-- 这里可写当前语言的特殊实现逻辑 -->
6060

6161
```python
62+
import pandas as pd
6263

63-
```
64-
65-
### **Java**
66-
67-
<!-- 这里可写当前语言的特殊实现逻辑 -->
68-
69-
```java
70-
71-
```
72-
73-
### **C++**
74-
75-
```cpp
76-
77-
```
78-
79-
### **Go**
80-
81-
```go
8264

65+
def modifySalaryColumn(employees: pd.DataFrame) -> pd.DataFrame:
66+
employees['salary'] *= 2
67+
return employees
8368
```
8469

8570
### **...**

solution/2800-2899/2884.Modify Columns/README_EN.md

+5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ DataFrame <code>employees</code>
5353
### **Pandas**
5454

5555
```python
56+
import pandas as pd
5657

58+
59+
def modifySalaryColumn(employees: pd.DataFrame) -> pd.DataFrame:
60+
employees['salary'] *= 2
61+
return employees
5762
```
5863

5964
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pandas as pd
2+
3+
4+
def modifySalaryColumn(employees: pd.DataFrame) -> pd.DataFrame:
5+
employees['salary'] *= 2
6+
return employees

solution/2800-2899/2885.Rename Columns/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ The column names are changed accordingly.</pre>
6666
<!-- 这里可写当前语言的特殊实现逻辑 -->
6767

6868
```python
69+
import pandas as pd
6970

71+
72+
def renameColumns(students: pd.DataFrame) -> pd.DataFrame:
73+
students.rename(columns={'id': 'student_id', 'first': 'first_name', 'last': 'last_name', 'age': 'age_in_years'},
74+
inplace=True)
75+
return students
7076
```
7177

7278
### **...**

solution/2800-2899/2885.Rename Columns/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ The column names are changed accordingly.</pre>
6060
### **Pandas**
6161

6262
```python
63+
import pandas as pd
6364

65+
66+
def renameColumns(students: pd.DataFrame) -> pd.DataFrame:
67+
students.rename(columns={'id': 'student_id', 'first': 'first_name', 'last': 'last_name', 'age': 'age_in_years'},
68+
inplace=True)
69+
return students
6470
```
6571

6672
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pandas as pd
2+
3+
4+
def renameColumns(students: pd.DataFrame) -> pd.DataFrame:
5+
students.rename(
6+
columns={
7+
'id': 'student_id',
8+
'first': 'first_name',
9+
'last': 'last_name',
10+
'age': 'age_in_years',
11+
},
12+
inplace=True,
13+
)
14+
return students

solution/2800-2899/2886.Change Data Type/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ The data types of the column grade is converted to int.</pre>
5656
<!-- 这里可写当前语言的特殊实现逻辑 -->
5757

5858
```python
59+
import pandas as pd
5960

61+
62+
def changeDatatype(students: pd.DataFrame) -> pd.DataFrame:
63+
students['grade'] = students['grade'].astype(int)
64+
return students
6065
```
6166

6267
### **...**

solution/2800-2899/2886.Change Data Type/README_EN.md

+5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ The data types of the column grade is converted to int.</pre>
5050
### **Pandas**
5151

5252
```python
53+
import pandas as pd
5354

55+
56+
def changeDatatype(students: pd.DataFrame) -> pd.DataFrame:
57+
students['grade'] = students['grade'].astype(int)
58+
return students
5459
```
5560

5661
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pandas as pd
2+
3+
4+
def changeDatatype(students: pd.DataFrame) -> pd.DataFrame:
5+
students['grade'] = students['grade'].astype(int)
6+
return students

solution/2800-2899/2887.Fill Missing Data/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ The quantity for Toaster and Headphones are filled by 0.</pre>
5555
<!-- 这里可写当前语言的特殊实现逻辑 -->
5656

5757
```python
58+
import pandas as pd
5859

60+
61+
def fillMissingValues(products: pd.DataFrame) -> pd.DataFrame:
62+
products['quantity'] = products['quantity'].fillna(0)
63+
return products
5964
```
6065

6166
### **...**

solution/2800-2899/2887.Fill Missing Data/README_EN.md

+5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ The quantity for Toaster and Headphones are filled by 0.</pre>
4949
### **Pandas**
5050

5151
```python
52+
import pandas as pd
5253

54+
55+
def fillMissingValues(products: pd.DataFrame) -> pd.DataFrame:
56+
products['quantity'] = products['quantity'].fillna(0)
57+
return products
5358
```
5459

5560
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pandas as pd
2+
3+
4+
def fillMissingValues(products: pd.DataFrame) -> pd.DataFrame:
5+
products['quantity'] = products['quantity'].fillna(0)
6+
return products

solution/2800-2899/2888.Reshape Data Concatenate/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ df1</strong>
7777
<!-- 这里可写当前语言的特殊实现逻辑 -->
7878

7979
```python
80+
import pandas as pd
8081

82+
83+
def concatenateTables(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame:
84+
return pd.concat([df1, df2], ignore_index=True)
8185
```
8286

8387
### **...**

0 commit comments

Comments
 (0)