C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Pandas DataFrame.fillna()We can use the fillna() function to fill the null values in the dataset. Syntax:
 DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs) Parameters:
 
 Returns:
 It returns an object in which the missing values are being filled. Example1:
import pandas as pd
# Create a dataframe
info = pd.DataFrame(data={'x':[10,20,30,40,50,None]})
print(info)
# Fill null value to dataframe using 'inplace'
info.fillna(value=0, inplace=True)
print(info)
Output        x
0     10.0
1     20.0
2     30.0
3     40.0
4     50.0
5     NaN
       x
0     10.0
1     20.0
2     30.0
3     40.0
4     50.0
5      0.0
Example2:The below code is responsible for filling the DataFrame that consist some NaN values. 
import pandas as pd
# Create a dataframe
info = pd.DataFrame([[np.nan,np.nan, 20, 0],
[1, np.nan, 4, 1],
[np.nan, np.nan, np.nan, 5],
[np.nan, 20, np.nan, 2]],
columns=list('ABCD'))
info
Output A B C D 0 NaN NaN 20.0 0 1 1.0 NaN 4.0 1 2 NaN NaN NaN 5 3 NaN 20.0 NaN 2 Example3:In below code, we have used the fillna function to fill in some of the NaN values only. 
info = pd.DataFrame([[np.nan,np.nan, 20, 0],
[1, np.nan, 4, 1],
[np.nan, np.nan, np.nan, 5],
[np.nan, 20, np.nan, 2]],
columns=list('ABCD'))
info
info.fillna(0)
info.fillna(method='ffill')
values = {'A': 0, 'B': 1, 'C': 2, 'D': 3}
info.fillna(value=values)
info.fillna(value=values, limit=1)
Output A B C D 0 0.0 1.0 20.0 0 1 1.0 NaN 4.0 1 2 NaN NaN 2.0 5 3 NaN 20.0 NaN 2 
Next TopicDataFrame.replace()
 
 |