{"id":1228,"date":"2024-01-01T13:00:00","date_gmt":"2024-01-01T13:00:00","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=1228"},"modified":"2024-01-03T08:44:41","modified_gmt":"2024-01-03T08:44:41","slug":"pandas-isna","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/pandas-isna\/","title":{"rendered":"Pandas DataFrame isna() Method Explained"},"content":{"rendered":"\n<p>There are two essential functions in the Pandas library are isna() and isnull(), which play significant roles in the process of data cleaning. These functions are commonly used to detect any NULL or NaN values in the DataFrames. In this article, we will learn everything you need to learn about the isna() function in Pandas DataFrame as a beginner.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is the isna() function in Pandas?<\/strong><\/h2>\n\n\n\n<p><strong>The isna() function in Pandas is designed to identify missing values within the given input. This could be a scalar or array-like object.<\/strong> The function returns a boolean result or an array of booleans indicating the presence or absence of missing values. It can be used to detect missing values (NaN) in a DataFrame or Series.&nbsp;<\/p>\n\n\n\n<p>We should always handle the missing values before any conversions or data manipulation. One of the best ways to deal with the missing values is by using the <a href=\"https:\/\/favtutor.com\/articles\/pandas-fillna-method\/\">fillna<\/a> method. <\/p>\n\n\n\n<p><strong>When the isna() function is called, it returns an object of the same size, indicating whether the values are NA\/NaN. &#8216;NA&#8217; stands for &#8216;Not Available&#8217; and is often used to denote missing values in pandas DataFrames.<\/strong><\/p>\n\n\n\n<p>The result is a DataFrame or Series of the same shape as the input, where each element is a boolean indicating whether the corresponding element in the original DataFrame or Series is missing.<\/p>\n\n\n\n<p>We will now learn how to use the isna() method on a scalar as well as a DataFrame.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using isna on Scalar Arguments<\/strong><\/h3>\n\n\n\n<p>When used on scalars in Pandas the isna() function returns a singular boolean. The syntax for applying isna() function to a series is quite simple:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Series.isna()<\/code><\/pre>\n\n\n\n<p>When applied to ndarrays (n-dimensional arrays), the function returns an ndarray of booleans. This simply means that the isna() function will result in an array with True and False values. If the element at the position is NaN the value is True in the resultant array and if the element at a position in the array is not NaN it will simply return False.<\/p>\n\n\n\n<p>Let us see 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\n\n# Create a sample Series\nseries = pd.Series([1, 2, None, 4, 5])\n\n# Check for missing values in the Series\nmissing_values = series.isna()\n\n# Display the result\nprint(&quot;Series with Missing Value Indicator:&quot;)\nprint(missing_values)<\/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>Series with Missing Value Indicator:\n0    False\n1    False\n2     True\n3    False\n4    False\n<\/code><\/pre>\n\n\n\n<p>Similarly, for indexes too, an ndarray of booleans is returned, representing the presence or absence of missing values in the indexed data. <\/p>\n\n\n\n<p><strong>If we want to check if certain indices in a Series are associated with missing values, we can create a boolean mask using the isna() function and then use that mask with specific indices.<\/strong><\/p>\n\n\n\n<p>Let us see an example of this as well:<\/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\n\n# Create a sample Series\ndata = pd.Series([1, 2, None, 4, 5])\n\n# Check for missing values in the Series\nmissing_values = data.isna()\n\n# Define a list of indices to check\nindices_to_check = [1, 2, 4]\n\n# Use the boolean mask to check if values at specific indices are missing\nfor index in indices_to_check:\n    if missing_values[index]:\n        print(f&quot;Value at index {index} is missing.&quot;)\n    else:\n        print(f&quot;Value at index {index} is not missing.&quot;)<\/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>Value at index 1 is not missing.\nValue at index 2 is missing.\nValue at index 4 is not missing.\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using isna on DataFrame<\/strong><\/h3>\n\n\n\n<p>The isna() method can also be used to detect the missing values in a DataFrame. Same as above the output will return the same type, containing booleans.The syntax is also very simple:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>DataFrame.isna()<\/code><\/pre>\n\n\n\n<p>The function can be applied to a DataFrame to create a new DataFrame where each element is True if the corresponding element in the original DataFrame is NaN, and False otherwise.<\/p>\n\n\n\n<p>You will understand it better with 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\n\n# Create a sample DataFrame\ndata = {'Column1': [1, 2, 3, None, 5],\n        'Column2': [10, 20, 30, 40, 50]}\ndf = pd.DataFrame(data)\n\n# Display the Original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Check for missing values in the DataFrame\nmissing_values = df.isna()\n\n# Display the result\nprint(&quot;DataFrame with Missing Value Indicator:&quot;)\nprint(missing_values)<\/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    Column1  Column2\n0      1.0       10\n1      2.0       20\n2      3.0       30\n3      NaN       40\n4      5.0       50\n\nDataFrame with Missing Value Indicator:\n   Column1  Column2\n0    False    False\n1    False    False\n2    False    False\n3     True    False\n4    False    False\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Distinction Between isna() and isnull()<\/strong><\/h3>\n\n\n\n<p>Like isna(), the isnull() function is also a method used to detect missing values. It operates similarly to isna() and is, in fact, an alias for it. The isnull() function, like the isna() function, returns a DataFrame with True and False values. A True value indicates a null or missing value, while a False value indicates a not null and not missing value.<\/p>\n\n\n\n<p>Let us see 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\n\n# Create a sample DataFrame\ndata = {'Column1': [1, 2, 3, None, 5],\n        'Column2': [10, 20, 30, 40, 50]}\ndf = pd.DataFrame(data)\n\n# Display the Original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Check for missing values in the DataFrame\nmissing_values = df.isnull()\n\n# Display the result\nprint(&quot;DataFrame with Missing Value Indicator:&quot;)\nprint(missing_values)<\/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    Column1  Column2\n0      1.0       10\n1      2.0       20\n2      3.0       30\n3      NaN       40\n4      5.0       50\n\nDataFrame with Missing Value Indicator:\n   Column1  Column2\n0    False    False\n1    False    False\n2    False    False\n3     True    False\n4    False    False\n<\/code><\/pre>\n\n\n\n<p>As shown by the examples above, both functions perform the same operation. Therefore, the difference is not in functionality but in naming. isnull() is essentially an alias for isna(), meaning they are interchangeable.<\/p>\n\n\n\n<p>Both functions return a DataFrame and are used to detect missing values. Each function returns a boolean same-sized object indicating if the values are NA. NA values, such as None or numpy.NaN, get mapped to True values. Everything else gets mapped to False values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we have learned about the isna() function provided by the Pandas library in Python. It is a very convenient and efficient method to detect any NaN values in a Series or a DataFrame. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are two essential functions in the Pandas library are isna() and isnull(), which play significant roles in the process of data cleaning. These functions are commonly used to detect any NULL or NaN values in the DataFrames. In this article, we will learn everything you need to learn about the isna() function in Pandas [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":1230,"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":[37],"class_list":["post-1228","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","tag-pandas"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1228","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=1228"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1228\/revisions"}],"predecessor-version":[{"id":1296,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1228\/revisions\/1296"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/1230"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=1228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=1228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=1228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}