{"id":546,"date":"2023-11-24T05:42:22","date_gmt":"2023-11-24T05:42:22","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=546"},"modified":"2023-11-25T08:52:37","modified_gmt":"2023-11-25T08:52:37","slug":"pandas-fillna-method","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/pandas-fillna-method\/","title":{"rendered":"Pandas DataFrame fillna() Method (with Examples)"},"content":{"rendered":"\n<p>When working with data, it&#8217;s common to encounter null values, which are represented as NaN (Not a Number) in Pandas DataFrames.&nbsp; To address this issue, Pandas provides the fillna() method, which allows users to replace NaN values with their desired values. In this article, we will explore the various parameters and methods available in Pandas DataFrame&#8217;s fillna() method and how to use it effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is the fillna() method?<\/strong><\/h2>\n\n\n\n<p><strong>The fillna() method in Pandas DataFrame is used to replace NaN values with a specified value.<\/strong> It&#8217;s a common operation when working with data, as missing values can create issues during analysis or modeling. It provides flexibility in handling missing data and allows users to customize the replacement process.&nbsp;<\/p>\n\n\n\n<p>By the way, we can also handle missing values by <a href=\"https:\/\/favtutor.com\/articles\/pandas-drop-column\/\">deleting the column<\/a> which contains them.<\/p>\n\n\n\n<p>Here is the syntax for the fillna() method is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None)<\/code><\/pre>\n\n\n\n<p>Here\u2019s a breakdown of the syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>value: <\/strong>This parameter specifies the value to replace the NaN values with. It can be a static value, a dictionary, an array, a Series, or even another DataFrame.<br><\/li>\n\n\n\n<li><strong>method:<\/strong> If the value parameter is not provided, the method parameter can be used to specify the method for filling the missing values. Pandas offers methods such as &#8216;backfill&#8217;, &#8216;bfill&#8217;, &#8216;pad&#8217;, &#8216;ffill&#8217;, or None.<br><\/li>\n\n\n\n<li><strong>axis:<\/strong> The axis parameter determines whether the replacement should be performed along the rows (axis=0) or columns (axis=1). By default, it is set to 0 (rows).<br><\/li>\n\n\n\n<li><strong>inplace<\/strong>: This boolean parameter determines whether the changes should be made directly to the original DataFrame. If set to True, the original DataFrame will be modified, while False (default) will return a new DataFrame with the replacements.<br><\/li>\n\n\n\n<li><strong>limit<\/strong>: The limit parameter specifies the maximum number of consecutive NaN values to fill if the method parameter is used. If not specified, it determines the maximum number of entries along the entire axis where NaNs will be filled.<br><\/li>\n\n\n\n<li><strong>downcast<\/strong>: This parameter allows for downcasting the data types of the DataFrame. It takes a dictionary or the string &#8216;infer&#8217; to automatically downcast to an appropriate equal type.<br><\/li>\n\n\n\n<li><strong>kwargs<\/strong>: This allows for any other keyword arguments to be passed to the method.<br><\/li>\n<\/ul>\n\n\n\n<p>Now, let us see the various methods to use the fillna() method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Replace the NaN with a Static Value<\/strong><\/h3>\n\n\n\n<p>One common scenario is replacing NaN values with a static value. This is useful when you want to replace all missing values with a specific value throughout the DataFrame. <\/p>\n\n\n\n<p>Let&#8217;s consider an example where we have a DataFrame df with a column named &#8220;city&#8221; that contains some NaN or NULL values. We can use the fillna() method to replace these NaN values with a static value.<\/p>\n\n\n\n<p>In the example, we will use the fillna() method to the &#8220;city&#8221; column of the df DataFrame. The NaN values in this column are replaced with the string &#8220;No City&#8221;. By setting the inplace parameter to True, the changes are made directly to the original DataFrame.<\/p>\n\n\n\n<p>Check the Python example below to replace the NaN with static value using fillna() function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\ndata = {'name': ['John', 'Jane', 'Jade', 'Jan'], 'age': [25, 30, 35, 40], 'city': [None, 'London', 'Paris', None]}\ndf = pd.DataFrame(data)\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Use fillna to replace the none value with a static value.\ndf[&quot;city&quot;].fillna(&quot;No City&quot;, inplace=True)\n\n# Display the modified DataFrame\nprint('Modified DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    name  age    city\n0  John   25    None\n1  Jane   30  London\n2  Jade   35   Paris\n3   Jan   40    None\n\n\nModified DataFrame:\n    name  age     city\n0  John   25  No City\n1  Jane   30   London\n2  Jade   35    Paris\n3   Jan   40  No City\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using the Method Parameter<\/strong><\/h3>\n\n\n\n<p><strong>The method parameter in the fillna() method allows for filling missing values using a specific method. Pandas provides several methods such as &#8216;backfill&#8217;, &#8216;bfill&#8217;, &#8216;pad&#8217;, &#8216;ffill&#8217;, or None.<\/strong> These methods determine how the replacement values are propagated within the DataFrame.&nbsp;<\/p>\n\n\n\n<p>In the above example, we use the fillna() to the &#8220;city&#8221; column of the df DataFrame using the <strong>&#8216;ffill&#8217;<\/strong> (forward fill) method.&nbsp;<\/p>\n\n\n\n<p>This method replaces the NaN values with the<strong> previous non-null<\/strong> value in the same column. Similarly, you can use<strong> &#8216;bfill&#8217;<\/strong> (backward fill) to replace NaN values with the <strong>next non-null <\/strong>value in the column.<\/p>\n\n\n\n<p>Here is an example to better understand it:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\ndata = {'name': ['John', 'Jane', 'Jade', 'Jan'], 'age': [25, 30, 35, 40], 'city': ['New York', None, 'Paris', None]}\ndf = pd.DataFrame(data)\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Use fillna method 'ffill' to replace the none value with a previous not null value.\ndf[&quot;city&quot;].fillna(method='ffill', inplace=True)\n\n# Display the modified DataFrame\nprint('Modified DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    name  age      city\n0  John   25  New York\n1  Jane   30      None\n2  Jade   35     Paris\n3   Jan   40      None\n\n\nModified DataFrame:\n    name  age      city\n0  John   25  New York\n1  Jane   30  New York\n2  Jade   35     Paris\n3   Jan   40     Paris\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using the Axis Parameter<\/strong><\/h3>\n\n\n\n<p><strong>The axis parameter in the fillna() method determines the axis along which the replacement should be performed. By default, it is set to 0, indicating that the replacement should be done along the rows. <\/strong>However, you can also specify axis=1 to perform the replacement along the columns. Let&#8217;s consider an example to illustrate the usage of the axis parameter.<\/p>\n\n\n\n<p>In the below example, the fillna() method is used to replace NaN values in the &#8220;city&#8221; column with the string &#8220;Unknown&#8221; along the columns. By setting the axis parameter to 1, the replacement is performed column-wise.<\/p>\n\n\n\n<p>Here is an examples in Python for using axis parameter:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\ndata = {'name': ['John', 'Jane', 'Jade', 'Jan'], 'age': [25, 30, 35, 40], 'city': ['New York', None, 'Paris', None]}\ndf = pd.DataFrame(data)\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Use fillna to fill missing values along columns (axis=1)\ndf = df.fillna(value='Unknown', axis=1)\n\n# Display the modified DataFrame\nprint('Modified DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    name  age      city\n0  John   25  New York\n1  Jane   30      None\n2  Jade   35     Paris\n3   Jan   40      None\n\n\nModified DataFrame:\n    name age      city\n0  John  25  New York\n1  Jane  30   Unknown\n2  Jade  35     Paris\n3   Jan  40   Unknown\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Modifying the DataFrame Inplace<\/strong><\/h3>\n\n\n\n<p>The fillna() method provides the flexibility to modify the DataFrame inplace or return a new DataFrame with the replacements. By default, the inplace parameter is set to False, indicating that a new DataFrame will be returned. However, if you want to modify the original DataFrame directly, you can set the inplace parameter to True. Let&#8217;s see an example to demonstrate this behavior.<\/p>\n\n\n\n<p>In the below example, the fillna() method is applied to the &#8220;city&#8221; column of the df DataFrame. By setting the inplace parameter to True, the changes are made directly to the original DataFrame.<\/p>\n\n\n\n<p>Here is an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\ndata = {'name': ['John', 'Jane', 'Jade', 'Jan'], 'age': [25, 30, 35, 40], 'city': ['New York', None, 'Paris', None]}\ndf = pd.DataFrame(data)\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Use fillna to fill missing values by modifying the DataFrame inplace\ndf.fillna(value={&quot;city&quot;: &quot;No City&quot;}, inplace=True)\n\n# Display the modified DataFrame\nprint('Modified DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    name  age      city\n0  John   25  New York\n1  Jane   30      None\n2  Jade   35     Paris\n3   Jan   40      None\n\n\nModified DataFrame:\n    name  age      city\n0  John   25  New York\n1  Jane   30   No City\n2  Jade   35     Paris\n3   Jan   40   No City\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Limiting the Number of Replacements<\/strong><\/h3>\n\n\n\n<p><strong>The limit parameter in the fillna() method allows you to specify the maximum number of consecutive NaN values to fill if the method parameter is used. <\/strong>This parameter is useful when you want to limit the number of replacements to a specific number. Let&#8217;s consider an example to understand the usage of the limit parameter.<\/p>\n\n\n\n<p>In the below example, the fillna() method is applied to the &#8220;city&#8221; column of the df DataFrame using the &#8216;ffill&#8217; method. The limit parameter is set to 1, which means only one consecutive NaN value will be replaced.&nbsp;<\/p>\n\n\n\n<p>This can be helpful when you want to control the extent of filling missing values. Check the code below:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\ndata = {'name': ['John', 'Jane', 'Jade', 'Jan'], 'age': [25, 30, 35, 40], 'city': ['New York', None, None, 'Paris']}\ndf = pd.DataFrame(data)\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Use fillna to fill upto one consecutive missing values \ndf[&quot;city&quot;].fillna(method='ffill', limit=1, inplace=True)\n\n# Display the modified DataFrame\nprint('Modified DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    name  age      city\n0  John   25  New York\n1  Jane   30      None\n2  Jade   35      None\n3   Jan   40     Paris\n\n\nModified DataFrame:\n    name  age      city\n0  John   25  New York\n1  Jane   30  New York\n2  Jade   35      None\n3   Jan   40     Paris\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Additional Parameters<\/strong><\/h3>\n\n\n\n<p>Apart from the primary parameters discussed above, the fillna() method also accepts additional keyword arguments (kwargs). These additional arguments allow for further customization and flexibility. Users can pass any other relevant keyword arguments specific to their use case.&nbsp;<\/p>\n\n\n\n<p>Let&#8217;s consider an example to illustrate the usage of additional parameters.<\/p>\n\n\n\n<p>In the below example, the fillna() method is applied to the &#8220;age&#8221; column of the df DataFrame. The NaN values in this column are replaced with the mean age of the column and the axis parameter is set to 0 to fill along the rows. Here is the Python code on how to use them:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\ndata = {'name': ['John', 'Jane', 'Jade', 'Jan'], 'age': [25, None, 35, None], 'city': ['New York', 'London', 'Delhi', 'Paris']}\ndf = pd.DataFrame(data)\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Use fillna to fill mean value in the missing values \ndf.fillna(value={&quot;age&quot;: df[&quot;age&quot;].mean()}, inplace=True, axis=0)\n\n# Display the modified DataFrame\nprint('Modified DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    name   age      city\n0  John  25.0  New York\n1  Jane   NaN    London\n2  Jade  35.0     Delhi\n3   Jan   NaN     Paris\n\nModified DataFrame:\n    name   age      city\n0  John  25.0  New York\n1  Jane  30.0    London\n2  Jade  35.0     Delhi\n3   Jan  30.0     Paris\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we explored the fillna() method in Pandas DataFrame, which provides a powerful tool for replacing NaN values with desired values.&nbsp;We discussed the various parameters available in the fillna() method, including value, method, axis, inplace, limit and other parameters.&nbsp; We also provided examples and use cases to demonstrate the practical application of it. By using the fillna() method effectively, you can handle missing data and ensure the completeness of your data analysis.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with data, it&#8217;s common to encounter null values, which are represented as NaN (Not a Number) in Pandas DataFrames.&nbsp; To address this issue, Pandas provides the fillna() method, which allows users to replace NaN values with their desired values. In this article, we will explore the various parameters and methods available in Pandas [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":549,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[35],"tags":[36,37],"class_list":["post-546","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","tag-data-science","tag-pandas"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=546"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/546\/revisions"}],"predecessor-version":[{"id":588,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/546\/revisions\/588"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/549"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}