Updated April 5, 2023
Introduction to Pandas DataFrame.fillna()
Handling Nan or None values is a very critical functionality when the data is very large. It is a more usual outcome that at most instances the larger datasets hold more number of Nan values in different forms, So standardizing these Nan’s to a single value or to a value which is needed is a critical process while handling larger datasets, The fillna() function is used for this purpose in pandas library. It verifies all the Nan values and replaces them with the assigned replacement value.
Syntax:
DataFrame.fillna(self, value=None, method=None, axis=None, inplace=False, limit=None, downcast=None)
Parameter & Description of Pandas DataFrame.fillna()
Below are the parameters of Pandas DataFrame.fillna() in Python:
Parameter | Description |
Value | Mentions the value which needs to be used for filling all the Nan, the needed values must be assigned to this value parameter. This parameter also provides the capability to insert a dict, series, or a dataframe of values to some specific indexes (for a Series data structure) or columns (For a dataframe data structure) in the targeted data entity. A list cannot be assigned to this object. |
Method | The method parameter represents the technique that needs to be used for filling the Nan’s in the dataframe. |
Axis | This argument represents the column or the axis upon which the fillna() function needs to be applied. The value specified in this argument represents either a column, position, or location in a dataframe. to achieve this capability to flexibly travel over a dataframe the axis value is framed on below means, {index (0), columns (1)}. here mentioning the value of 0 to axis argument fills the Nan value for each and every row in the dataframe, whereas mentioning the value of 1 in the dataframe fills the Nan value for all the columns in the dataframe. |
Data | Defines the data of the row. |
Inplace | This is used to determine whether the operation needs to be performed at the place of the data. So this means whether the outcome of the fillna needs to be performed directly on to the current Dataframe for which it is applied. This is again a boolean variable, if this is set to true then the fillna process will be applied to the current dataframe itself, if this argument is assigned as false then no changes will be applied to the current dataframe a equals relation can be used to pull the updated dataframe values into a different dataframe. |
Limit | This mentions the overall number of Nan values that are allowed to be filled backward and forward. So if there is a gap in the number of Nan’s for a specific series then the Nan filling process will be partially performed. |
Downcast | How to downcast a given value from its currently specified datatype if it is possible to be performed. |
Examples to Implement of Pandas DataFrame.fillna()
Below are the examples of Pandas DataFrame.fillna():
Example #1
Code:
import pandas as pd
import numpy as np
Core_SERIES = pd.Series([ 'A', 'B', np.nan, 'D', np.nan, 'F'])
print(" THE CORE SERIES ")
print(Core_SERIES)
print("")
print(Core_SERIES.fillna('No Value'))
Output:
Explanation: In this example, the core Series is first formulated. The pd.Series() method is used for formulating the Series. A set of alphabets from A to F is inserted as input to the series. A couple of indexes in-between this series is associated with value Nan, here NumPy library is used for making these Nan values in place, The fillna() function offers the flexibility to sophisticatedly iterate through these indexes of the series and replace every Nan value with the corresponding replace value which is specified. Here, in this case, the replace value is a string namely ‘ No Value ‘. The Generated output dataframe after the insert is printed onto the console. we can notice the Nan values are nicely being replaced with the corresponding string ‘No Value’.
Example #2
Code:
import pandas as pd
import numpy as np
Core_Dataframe = pd.DataFrame({'Emp_No' : ['Emp1', np.nan,'Emp3','Emp4'],
'Employee_Name' : ['Arun', 'selva', np.nan, 'arjith'],
'Employee_dept' : ['CAD', 'CAD', 'DEV', np.nan]})
print(" THE CORE DATAFRAME BEFORE FILLNA ")
print(Core_Dataframe)
print("")
print(" THE CORE DATAFRAME AFTER FILLNA ")
print(Core_Dataframe.fillna({'Emp_No' : 0 ,
'Employee_Name' : ' No Value ' ,
'Employee_dept' : 'No Value'
}))
print("")
Output:
Explanation: In this example, the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe is inserted along with their column names. Once the dataframe is completely formulated it is printed on to the console. We can notice at this instance the dataframe holds details like employee number, employee name, and employee department. Here some among the indexes are inserted with Nan values using numpy library, The fillna() process is applied in a column manner, the Nan’s in employee number column is filled as 0, the Nan’s in employee Name column is filled as ‘No Value’ and the Nan’s in employee dept column is also filled as ‘No Value’. We can notice from the console output that the expected indexes are replaced accordingly.
Example #3
Code:
import pandas as pd
import numpy as np
Core_Dataframe = pd.DataFrame({'A' : [ 1, 6, 11, 15, 21, 26],
'B' : [2, 7, 12, 17, 22, 27],
'C' : [3, np.nan, 13, 18, 23, 28],
'D' : [4, 9, 14, 19, np.nan, 29],
'E' : [5, 10, 15, np.nan, 25, 30]})
print(" THE CORE DATAFRAME BEFORE FILLNA")
print(Core_Dataframe)
print("")
print(" THE CORE DATAFRAME AFTER FILLNA")
Core_Dataframe.fillna(0,axis=1,inplace=True)
print(Core_Dataframe)
Output:
Explanation: In this example, the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe are inserted along with their column names. Once the dataframe is completely formulated it is printed on to the console. We can notice at this instance the dataframe holds a random set of numbers and alphabetic values of columns associated with it. The fillna() method is used in such a way here that all the Nan values are replaced with zeroes.
Recommended Articles
We hope that this EDUCBA information on “Pandas DataFrame.fillna()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.