Filling missing values in pandas



First we want to create the dictionary.Next we want to convert the dictionary into dataframe by using pd.dataframe in the pandas concepts for that we want to import the pandas.For the missing value we can replace the any values by using the fillna() function.








Example-1
import pandas as pd
import numpy as np

d = { 'Score_1' : [ 10, 20, 30, np.nan],
 'Score_2' : [ 40, 50, np.nan, 60],
'Score_3' : [ np.nan, 70, 80, 90]}

n = pd.DataFrame(d)

print(n.fillna(0))



Output



   Score_1  Score_2  Score_3
0     10.0     40.0      0.0
1     20.0     50.0     70.0
2     30.0      0.0     80.0
3      0.0     60.0     90.0







Example-2
import pandas as pd
import numpy as np

d = { 'Score_1' : [np.nan , 5, 10, 15],
'Score_2' : [ 20, 25, np.nan, 30],
'Score_3' : [ 35, np.nan, 45, 50]}

n = pd.DataFrame(d)

print(n.fillna(0))



Output



   Score_1  Score_2  Score_3
0      0.0     20.0     35.0
1      5.0     25.0      0.0
2     10.0      0.0     45.0
3     15.0     30.0     50.0