1 minute read

1. Read a excel file : read_excel()

import pandas as pd

df1 = pd.read_excel("E04EXAMPLE.xlsx", sheet_name=1)
df1
Name Mark
0 Louis 100.0
1 Harvey 59.0
2 G-dragon NaN
3 Lola 87.0
4 Jorge 90.0
5 Piona 54.0
6 Mitchy 93.0
7 Fibio 94.0
8 Kim NaN
9 Stacy 71.0
10 Grace 96.0
11 TL NaN
12 Sanchez 80.0
13 Zhen 86.0
14 James 84.0
15 Coline NaN
16 Gorila 52.0
17 Sunny 77.0
18 Conner 84.0
19 Sally 52.0
20 Marry 70.0
21 Katy 55.0
22 Gerge NaN
23 kipling 76.0
24 Guggi NaN

2. Checking missing values

df1.isnull()
Name Mark
0 False False
1 False False
2 False True
3 False False
4 False False
5 False False
6 False False
7 False False
8 False True
9 False False
10 False False
11 False True
12 False False
13 False False
14 False False
15 False True
16 False False
17 False False
18 False False
19 False False
20 False False
21 False False
22 False True
23 False False
24 False True
df1.isnull().sum().sum()>0
True
df1.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 25 entries, 0 to 24
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   Name    25 non-null     object 
 1   Mark    19 non-null     float64
dtypes: float64(1), object(1)
memory usage: 528.0+ bytes

3. Imputation : fillna()

df2 = df1.fillna(0)
df2
Name Mark
0 Louis 100.0
1 Harvey 59.0
2 G-dragon 0.0
3 Lola 87.0
4 Jorge 90.0
5 Piona 54.0
6 Mitchy 93.0
7 Fibio 94.0
8 Kim 0.0
9 Stacy 71.0
10 Grace 96.0
11 TL 0.0
12 Sanchez 80.0
13 Zhen 86.0
14 James 84.0
15 Coline 0.0
16 Gorila 52.0
17 Sunny 77.0
18 Conner 84.0
19 Sally 52.0
20 Marry 70.0
21 Katy 55.0
22 Gerge 0.0
23 kipling 76.0
24 Guggi 0.0

4. Deletion : dropna()

df3 = df1.dropna()
df3
Name Mark
0 Louis 100.0
1 Harvey 59.0
3 Lola 87.0
4 Jorge 90.0
5 Piona 54.0
6 Mitchy 93.0
7 Fibio 94.0
9 Stacy 71.0
10 Grace 96.0
12 Sanchez 80.0
13 Zhen 86.0
14 James 84.0
16 Gorila 52.0
17 Sunny 77.0
18 Conner 84.0
19 Sally 52.0
20 Marry 70.0
21 Katy 55.0
23 kipling 76.0

5. Coppy to clipboard : to_clipboard()

df2.to_clipboard(index=False)
df3.to_clipboard(index=False)

6. Total code

import pandas as pd

df1 = pd.read_excel("E04EXAMPLE.xlsx", sheet_name=1)
df2 = df1.fillna(0)
df3 = df1.dropna()

df2.to_clipboard(index=False)
df3.to_clipboard(index=False)

Updated: