How to create empty python framework where df.empty results in True

How can I create an empty framework in python to test the df.empty

result True

?

I've tried this:

df = pd.DataFrame(np.empty((1, 1)))

      

and df.empty

leads to False

.

+3


source to share


1 answer


The simplest is pd.DataFrame()

:

df = pd.DataFrame()   

df.empty
# True

      

If you want to create a dataframe to specify the number of columns:



df = pd.DataFrame(columns=['A', 'B'])

df.empty
# True

      

Also, an array of shape (1, 1) is not empty (it has one line), which is why you get empty = False, to create an empty array it must have shape (0, n):

df = pd.DataFrame(pd.np.empty((0, 3)))

df.empty
# True

      

+6


source







All Articles