First create a with missing values NaN(Not A Number) of CSV(comma-separated values) file :
import pandas as pdfrom io import StringIOcsv_data =
"""A,B,C,D1.0,2.0,3.0,4.05.0,6.0,,8.00.0,11.0,12.0,"""# If you are using Python
2.7, you need# to convert the string to unicode:# csv_data =
unicode(csv_data)df = pd.read_csv(StringIO(csv_data))1
2
3
4
5
6
7
8
9
10
Delete samples with missing values
Specific treatment method :
df.isnull()# Is the missing value returned True, Otherwise range False
df.isnull().sum()# Returns the number of missing values contained in each column
df.dropna()# Directly delete rows with missing values
df.dropna(axis = 1)# Directly delete columns with missing values
df.dropna(how = "all")# Delete only rows with all missing values
df.dropna(thresh = 4)# Keep at least 4 Rows with missing values
df.dropna(subset = ["C"])# Delete specific columns with missing values
Fill in missing values
Numerical value (Numerical Data)
Method 1 :fillna() function
df.fillna(0): use 0 fill
df.fillna(method="pad"): Fill with the previous value
df.fillna(df2.mean()): Fill with the column mean
Method 2 :Imputer
from sklearn.preprocessing import Imputerimr = Imputer(missing_values="NaN",
strategy="mean", axis=0)# Mean fill missing value imr = imr.fit(df)imputed_data =
imr.transform(df.values)1
2
3
4
Technology