-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMissingValues.py
128 lines (85 loc) · 3.47 KB
/
MissingValues.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 1 15:50:25 2020
@author: Abhinav
"""
#--------------------------Handling Missing Values----------------------------
#Counting the Missing Values---------------------------
import pandas as pd
import numpy as np
#Create a DataFrame
df1 = {'Subject':['semester1','semester2','semester3','semester4','semester1',
'semester2','semester3'],
'Score':[62,47,np.nan,74,np.nan,77,85]}
df1 = pd.DataFrame(df1,columns=['Subject','Score'])
print(df1)
'''Is there any missing values in dataframe '''
df1.isnull()
df1.notnull()
'''Is there any missing values across columns'''
df1.isnull().any()
'''How many missing values are there across each column'''
df1.isnull().sum()
#Dropping rows with Missing Values-----------------------
import pandas as pd
import numpy as np
#Create a DataFrame
df1 = {'Name':['George','Andrea','micheal','maggie','Ravi','Xien','Jalpa',np.nan],
'State':['Arizona','Georgia','Newyork','Indiana','Florida','California',np.nan,np.nan],
'Gender':["M","F","M","F","M","M",np.nan,np.nan],
'Score':[63,48,56,75,np.nan,77,np.nan,np.nan]}
df1 = pd.DataFrame(df1,columns=['Name','State','Gender','Score'])
print(df1)
#Drop all rows that have any NaN (missing) values
df1.dropna()
#Drop only if entire row has NaN values
df1.dropna(how='all')
#Drop only if a row has more than 2 NaN values
df1.dropna(thresh=2)
#Drop NaN in a specific column
df1.dropna(subset=['Gender'])
df2 = df1.dropna(subset=['Gender','Score'])
df2
#Dropping rows using axis values:
df1
df1.dropna(axis=0)
#Dropping columns using axis values:
df1.dropna(axis=1)
#------------------Creating Data Frame Again-----------------------------------
df1 = {'Name':['George','Andrea','micheal','maggie','Ravi','Xien','Jalpa',np.nan],
'State':['Arizona','Georgia','Newyork','Indiana','Florida','California',np.nan,np.nan],
'Gender':["M","F","M","F","M","M",np.nan,np.nan],
'Score':[63,48,56,75,np.nan,77,np.nan,np.nan]}
df1 = pd.DataFrame(df1,columns=['Name','State','Gender','Score'])
print(df1)
#------------------Replacing Missing Values with Zero--------------------------
df1
df1.fillna(0)
#-----------------Replacing Missing Values with Mean of the column-------------
df1
df1["Score"].fillna(df1["Score"].mean(), inplace=True)
print(df1)
#----------------Replacing Missing Value with Median of the column-------------
df1["Score"].fillna(df1["Score"].median(), inplace=True)
print(df1)
#Replace Missing (or) Generic Values using replace() method
#Many times, we have to replace a generic value with some specific value.
#We can achieve this by applying the replace method.
df = pd.DataFrame({'one':[10,20,30,40,50,2000], 'two':[1000,0,30,40,50,60]})
print(df)
print (df.replace({1000:10,2000:60}))
#------------------Handling Duplicate Values--------------------------------
#The drop_duplicates() function performs common data cleaning task that deals with duplicate values
#in the DataFrame. This method helps in removing duplicate values from the DataFrame.
emp = {"Name": ["Parker", "Smith", "William", "Parker"],
"Age": [21, 32, 29, 21]}
info = pd.DataFrame(emp)
print(info)
info = info.drop_duplicates()
print(info)
emp = {"Name": ["Parker", "Smith", "William", "Parker"],
"Age": [21, 32, 29, 22]}
info = pd.DataFrame(emp)
print(info)
info = info.drop_duplicates()
print(info)