#importing libraries
import pandas as pd
import numpy as np
Note: Missing data in the Pandas is represented by the value NaN (Not a Number)
s=pd.Series(["A",np.nan,"C","B",np.nan,"E",np.nan,np.nan,"D"])
s
0 A 1 NaN 2 C 3 B 4 NaN 5 E 6 NaN 7 NaN 8 D dtype: object
s.isnull()
0 False 1 True 2 False 3 False 4 True 5 False 6 True 7 True 8 False dtype: bool
s[s.isnull()]
1 NaN 4 NaN 6 NaN 7 NaN dtype: object
s.notnull()
0 True 1 False 2 True 3 True 4 False 5 True 6 False 7 False 8 True dtype: bool
s[s.notnull()]
0 A 2 C 3 B 5 E 8 D dtype: object
s.dropna()
0 A 2 C 3 B 5 E 8 D dtype: object
df=pd.DataFrame([[12,32,13,55,78,54],[42,np.nan,54,np.nan,55,855],[np.nan,64,np.nan,45,np.nan,78],[55,447,552,np.nan,85,889]])
df
| 0 | 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|---|
| 0 | 12.0 | 32.0 | 13.0 | 55.0 | 78.0 | 54 |
| 1 | 42.0 | NaN | 54.0 | NaN | 55.0 | 855 |
| 2 | NaN | 64.0 | NaN | 45.0 | NaN | 78 |
| 3 | 55.0 | 447.0 | 552.0 | NaN | 85.0 | 889 |
df.dropna()
| 0 | 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|---|
| 0 | 12.0 | 32.0 | 13.0 | 55.0 | 78.0 | 54 |
df.dropna(axis=1)
| 5 | |
|---|---|
| 0 | 54 |
| 1 | 855 |
| 2 | 78 |
| 3 | 889 |
df.fillna(0)
| 0 | 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|---|
| 0 | 12.0 | 32.0 | 13.0 | 55.0 | 78.0 | 54 |
| 1 | 42.0 | 0.0 | 54.0 | 0.0 | 55.0 | 855 |
| 2 | 0.0 | 64.0 | 0.0 | 45.0 | 0.0 | 78 |
| 3 | 55.0 | 447.0 | 552.0 | 0.0 | 85.0 | 889 |
df.fillna({0:15,1:25,2:35,3:48,4:66,5:56})
| 0 | 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|---|
| 0 | 12.0 | 32.0 | 13.0 | 55.0 | 78.0 | 54 |
| 1 | 42.0 | 25.0 | 54.0 | 48.0 | 55.0 | 855 |
| 2 | 15.0 | 64.0 | 35.0 | 45.0 | 66.0 | 78 |
| 3 | 55.0 | 447.0 | 552.0 | 48.0 | 85.0 | 889 |
df.fillna(method="ffill")
| 0 | 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|---|
| 0 | 12.0 | 32.0 | 13.0 | 55.0 | 78.0 | 54 |
| 1 | 42.0 | 32.0 | 54.0 | 55.0 | 55.0 | 855 |
| 2 | 42.0 | 64.0 | 54.0 | 45.0 | 55.0 | 78 |
| 3 | 55.0 | 447.0 | 552.0 | 45.0 | 85.0 | 889 |
df.fillna(method="bfill")
| 0 | 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|---|
| 0 | 12.0 | 32.0 | 13.0 | 55.0 | 78.0 | 54 |
| 1 | 42.0 | 64.0 | 54.0 | 45.0 | 55.0 | 855 |
| 2 | 55.0 | 64.0 | 552.0 | 45.0 | 85.0 | 78 |
| 3 | 55.0 | 447.0 | 552.0 | NaN | 85.0 | 889 |