1# Six(6) ways to handle NaN values
2
3# 1. Drop/delete any rows with NaN values
4df.dropna(axis = 0) #row is axis = 0
5# 2. Drop/delete any columns with NaN values
6df.dropna(axis = 1) #column is axis = 1
7# 3. Replace all NaN values with 0
8df.fillna(0)
9# 4. Replace NaN values with the previous value in the column, Fill Forward
10df.fillna(method = 'ffill', axis = 0) #OR axis = 1 for rows
11# 5. Replace NaN values with the next value in the column, Fill Backward
12df.fillna(method = 'backfill', axis = 0) #OR axis =1 for rows
13# 6. replace NaN values by using linear interpolation using column values
14df.interpolate(method = 'linear', axis = 0) #OR axis = 1 for rows
15
16#NB: 1. For the last three options, depending on the method, changes to NaN
17# in the first row, last row, first column or last column may not be effected.
18# 2. Remember to include inplace = True if you want the original dataframe to
19#be modified, else the changes will revert back to the original when you
20#reference the dataframe again. Eg.
21df.dropna(axis = 0, inplace = True)