Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, and makes importing and analyzing data much easier.
Sometimes csv file has null values, which are later displayed as NaN in Data Frame. Just like pandas
dropna() method manage and remove Null values from a data frame,
fillna() manages and let the user replace NaN values with some value of their own.
DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)
value : Static, dictionary, array, series or dataframe to fill instead of NaN.
method : Method is used if user doesn’t pass any value. Pandas has different methods like
ffillwhich fills the place with value in the Forward index or Previous/Back respectively.
axis: axis takes int or string value for rows/columns. Input can be 0 or 1 for Integer and ‘index’ or ‘columns’ for String
inplace: It is a boolean which makes the changes in data frame itself if True.
limit : This is an integer value which specifies maximum number of consequetive forward/backward NaN value fills.
downcast : It takes a dict which specifies what dtype to downcast to which one. Like Float64 to int64.
**kwargs : Any other Keyword arguments
For link to CSV file Used in Code, click here.
Example #1: Replacing NaN values with a Static value.
In the following example, all the null values in College column has been replaced with “No college” string. Firstly, the data frame is imported from CSV and then College column is selected and
fillna() method is used on it.
Example #2: Using method Parameter
In the following example, method is set as ffill and hence the value in the same column replaces the null value. In this case Georgia State replaced null value in college column of row 4 and 5.
Similarly, bfill, backfill and pad methods can also be used.
Example #3: Using Limit
In this example, a limit of 1 is set in the fillna() method to check if the function stops replacing after one successful replacement of NaN value or not.
As shown in the output, The college column of 4th row was replaced but 5th one wasn’t since the limit was set 1.
- Replace values in Pandas dataframe using regex
- Replace NaN Values with Zeros in Pandas DataFrame
- Replace all the NaN values with Zero's in a column of a Pandas dataframe
- Mapping external values to dataframe values in Pandas
- Highlight the negative values red and positive values black in Pandas Dataframe
- Python | Pandas Series.str.replace() to replace text in a series
- Python | Pandas dataframe.replace()
- Replace Negative Number by Zeros in Pandas DataFrame
- Replace the column contains the values 'yes' and 'no' with True and False In Python-Pandas
- Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array
- Convert given Pandas series into a dataframe with its index as another column on the dataframe
- Python | Pandas DataFrame.values
- Get unique values from a column in Pandas DataFrame
- Get n-smallest values from a particular column in Pandas DataFrame
- Get n-largest values from a particular column in Pandas DataFrame
- Getting Unique values from a column in Pandas dataframe
- Using dictionary to remap values in Pandas DataFrame columns
- How to Drop Rows with NaN Values in Pandas DataFrame?
- Ways to Create NaN Values in Pandas DataFrame
- Drop rows from Pandas dataframe with missing values or NaN in columns
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.