{"id":6212,"date":"2021-05-22T09:06:14","date_gmt":"2021-05-22T03:36:14","guid":{"rendered":"https:\/\/python-programs.com\/?p=6212"},"modified":"2021-11-22T18:40:47","modified_gmt":"2021-11-22T13:10:47","slug":"pandas-4-ways-to-check-if-a-dataframe-is-empty-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/pandas-4-ways-to-check-if-a-dataframe-is-empty-in-python\/","title":{"rendered":"Pandas : 4 Ways to check if a DataFrame is empty in Python"},"content":{"rendered":"

How to check if a dataframe is empty in Python ?<\/h2>\n

In this article we will see different ways to check if a dataframe is empty in python.<\/p>\n

Approach-1 : Check whether dataframe is empty using Dataframe.empty :<\/h3>\n

There is an empty<\/code> attribute provided by dataframe class in python.<\/p>\n

Syntax - Dataframe.empty<\/pre>\n

If it returns True then the dataframe is empty.<\/p>\n

# Program :\r\n\r\nimport pandas as pd\r\n\r\n# empty Dataframe created\r\ndfObj = pd.DataFrame(columns=['Date', 'UserName', 'Action'])\r\n\r\n# Checking if Dataframe is empty or not\r\n# using empty attribute\r\nif dfObj.empty == True:\r\n    print('DataFrame is empty')\r\nelse:\r\n    print('DataFrame is not empty')<\/pre>\n
Output :\r\nDataFrame is not empty<\/pre>\n

Even it contains NaN then also it returns the dataframe is empty.<\/p>\n

# Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n\r\n# List of Tuples\r\nstudents = [(np.NaN, np.NaN, np.NaN),\r\n            (np.NaN, np.NaN, np.NaN),\r\n            (np.NaN, np.NaN, np.NaN)\r\n           ]\r\n\r\n# Dataframe object created\r\ndfObj = pd.DataFrame(columns=['Your Name', 'Your Age', 'Your City'])\r\n\r\n# Checking if Dataframe is empty or not\r\n# using empty attribute\r\nif dfObj.empty == True:\r\n    print('DataFrame is empty')\r\nelse:\r\n    print('DataFrame is not empty')<\/pre>\n
Output :\r\nDataFrame is empty<\/pre>\n

Approach-2 : Check if dataframe is empty using Dataframe.shape :<\/h3>\n

There is an shape attribute provided by dataframe class in python.<\/p>\n

Syntax- Dataframe.shape<\/pre>\n

shape attribute return a tuple containing dimension of dataframe. Like if in the dataframe there is 3 rows and 4 columns then it will return (3,4). If the dataframe is empty then it will return 0 at 0th index.<\/p>\n

# Create an empty Dataframe\r\ndfObj = pd.DataFrame(columns=['Date', 'UserName', 'Action'])\r\n# Check if Dataframe is empty using dataframe's shape attribute\r\nif dfObj.shape[0] == 0:\r\n    print('DataFrame is empty')\r\nelse:\r\n    print('DataFrame is not empty')<\/pre>\n
Output :\r\nDataFrame is empty<\/pre>\n

Approach-3 : Check if dataframe is empty by checking length of index :<\/h3>\n

Dataframe.index<\/code> represents indices of Dataframe. If the dataframe is empty then size will be 0.<\/p>\n

# Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n\r\n# empty Dataframe object created\r\ndfObj = pd.DataFrame(columns=['Date', 'UserName', 'Action'])\r\n# checking if length of index is 0 or not\r\nif len(dfObj.index.values) == 0:\r\n    print('DataFrame is empty')\r\nelse:\r\n    print('DataFrame is not empty')<\/pre>\n
Output :\r\nDataFrame <\/span>is<\/span> empty<\/span><\/pre>\n

Approach-4 : Check if dataframe is empty by using len on Datafarme :<\/h3>\n

Directly by calling the len()<\/code> function we can also check the dataframe is empty or not. If the length of dataframe is 0 then it the dataframe is empty.<\/p>\n

# Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n\r\n# empty Dataframe object created\r\ndfObj = pd.DataFrame(columns=['Date', 'UserName', 'Action'])\r\n\r\n# checking if length of dataframe is 0 or not\r\n# by calling len()\r\nif len(dfObj) == 0:\r\n    print('DataFrame is empty')\r\nelse:\r\n    print('DataFrame is not empty')<\/pre>\n
Output :\r\nDataFrame is not empty<\/pre>\n

Want to expert in the python programming language? Exploring\u00a0Python Data Analysis using Pandas<\/a>\u00a0tutorial changes your knowledge from basic to advance level in python concepts.<\/p>\n

Read more Articles on Python Data Analysis Using Padas \u2013 Find Elements in a Dataframe<\/strong><\/p>\n