Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create python empty dataframe where df.empty results in True

How can I create an empty dataframe in python such that the test df.empty results True?

I tried this:

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

and df.empty results in False.

like image 350
Goofball Avatar asked Apr 23 '17 16:04

Goofball


People also ask

How do you create an empty data frame in Python?

You can create an empty dataframe by importing pandas from the python library. Later, using the pd. DataFrame(), create an empty dataframe without rows and columns as shown in the below example.

How do you fill an empty DataFrame in Python?

Fill Data in an Empty Pandas DataFrame by Appending Rows First, create an empty DataFrame with column names and then append rows one by one. The append() method can also append rows. When creating an empty DataFrame with column names and row indices, we can fill data in rows using the loc() method.

How do I create an empty DataFrame with headers?

Use pandas. DataFrame() to create an empty DataFrame with column names. Call pandas. DataFrame(columns = column_names) with column set to a list of strings column_names to create an empty DataFrame with column_names .


1 Answers

The simplest is pd.DataFrame():

df = pd.DataFrame()   

df.empty
# True

If you want create a data frame of specify number of columns:

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

df.empty
# True

Besides, an array of shape (1, 1) is not empty (it has one row), which is the reason you get empty = False, in order to create an empty array, it needs to be of shape (0, n):

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

df.empty
# True
like image 146
Psidom Avatar answered Oct 19 '22 02:10

Psidom