gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
dataframe is empty check
- User
dataframe is empty check
- Assistant
In Python, using the
pandaslibrary, you can easily check if a DataFrame is empty by using the.emptyattribute. Here's how you can do it:pythonimport pandas as pd # Example of creating an empty DataFrame df = pd.DataFrame() # Check if the DataFrame is empty if df.empty: print("The DataFrame is empty.") else: print("The DataFrame is not empty.")In this example,
df.emptywill returnTrueif the DataFrame has no elements (i.e., it has zero rows and zero columns), andFalseotherwise.You can also check the shape of the DataFrame to determine if it's empty:
pythonif df.shape[0] == 0: print("The DataFrame is empty.") else: print("The DataFrame is not empty.")This checks whether the number of rows (
shape[0]) is zero, which indicates that the DataFrame is empty.