Pandas Create Empty Dataframe

A DataFrame consists of a two-dimensional data structure that features in Python and is made accessible by the pandas module, which holds content in a tabular manner. In other words, columns & rows. Every column in a DataFrame may hold a varied kind of item.

We may prefer to construct an empty DataFrame frequently to conserve storage. As opposed to adding the complete DataFrame, you may just need to add data using two entries. For instance, a blank DataFrame can be built initially. Afterward, the contents can be added a little at a time. A pandas dataframe object that is empty, or devoid of any data, and all of its dimensions are 0 lengths is known as an empty dataframe. Either zero rows or zero columns should exist in it.

The “DataFrame.empty” attribute from the Pandas DataFrame object allows us to determine if an object’s contents are empty or not. This property’s application on a Pandas DataFrame object yields a Boolean value, either True or False, based on the circumstance, or whether the relevant DataFrame object is blank or not.

Let’s learn over constructing an empty DataFrame and then adding rows and columns to it using Python’s Pandas module. The preceding methods in Python can be used to build an empty Pandas DataFrame. We will examine each of them in detail here.

Example #1: Constructing Empty DataFrame Using Pandas.DataFrame() Method

The basic and easiest method to create an empty dataframe is to create it without any rows and columns. To utilize the pandas function, we must first import Python’s pandas package. Then, in the script, pandas are referred to as “pd” by using them as pd language. Now, that we have access to the pandas’ library, we can begin creating our basic empty DataFrame.

The first script generates a new variable called “my_df” and assigns the result of calling the pd.DataFrame() method to it. Here, we have used the function pd.DataFrame() of the Pandas DataFrame class without any arguments, which will generate an empty pandas DataFrame object. We then employed the first print function to print a text. We will do so by writing the text inside the parenthesis of the print statement but we need to put it between inverted commas as we want to display as we put it. In the next line of the script, we have utilized another print function and inside its braces, we have put the variable “my_df”; holding the values of the DataFrame.

Execution of the above-mentioned code snippet will yield us the following output.

For the need to verify if the content of the DataFrame is empty or not we will use the .empty attribute.

We have again employed the print function and defined a text that says “if the DataFrame is empty this function will print True else print False.” This means that the .empty attribute will check if the DataFrame is empty or not. Based on the verification, it will return a Boolean value; either True or False. In the last print function, we have used the name of our variable with the .empty attribute as my_df.empty.

The output image below displays a “True” Boolean value which verifies that the DataFrame is empty.

Example # 2: Constructing Empty DataFrame with Columns Using Pandas.DataFrame() Method

Utilizing the “pd.DataFrame()” method, you can construct a Pandas DataFrame object that is empty and just has columns. We use a single argument to call the Pandas DataFrame class function, which then produces a blank Pandas DataFrame object with the supplied columns list.

Let us now use Python script to put this concept into exercise.

In this code, we have initialized a variable “d1″ and put it equal to the outcome of the pandas DataFrame function. Inside the brackets of the pd.DataFrame() function, we will pass a single argument “column” and assign it three values: name, age, and gender. The print function is invoked with a text to display inside its braces. This will be displayed before the dataframe itself. In the next print function, the variable “d1” is called.

Now, we’ll see if the dataframe is empty. But before that, we have defined another text inside the print function which will be exhibited before the Boolean value. Then, we called the .empty property with the variable “d1” to verify the emptiness of the dataframe stored in it, inside the last print function of the Python script.

When we run the above code, the output terminal shows a screen with an empty dataframe with a column. Here the index is empty, which also refers to the rows. Also, the Boolean value True in the last line of the terminal verifies that the DataFrame is empty.

We can utilize another method in which we will first create an empty dataframe and then append columns to it one by one.

Here, we have first created an empty DataFrame with the pd.DataFrame() function and print it. Then, we appended the names of the column in the dataframe one after another. For this, we have written the variable name “d1” and used brackets with it. Inside the brackets, we write the names of the columns and assign them no-values. In the last step, we checked if the provided dataframe is empty or not.

The output can be seen in the following image:

Example # 3: Constructing Empty DataFrame with Rows Using Pandas.DataFrame() Method

Utilizing the pd.DataFrame() method, is another simple method for generating a pandas DataFrame object that is blank and only includes rows. This function invokes the function pd.DataFrame of the Pandas DataFrame object with a single argument, returning an empty Pandas DataFrame object containing the rows or index list that was specified.

A variable named “d2” has been initialized in the above program and set to the output of the pandas DataFrame method. We will provide a single parameter, “index,” and assign it five values to the pd.DataFrame() function inside brackets: 1, 2, 3, 4, and 5. Given a text to show within its brackets, the print method is called. The variable “d2” is then accessed in the subsequent print function call. To check the Dataframe’s empty state, we then invoked the .empty attribute for the variable “d2” within the final print method of the Python code.

Once the aforementioned code is executed, a window with an empty dataframe with rows/index appears on the terminal. While the columns are empty in this instance, the index in this case has five values that correspond to the rows.

Example # 4: Constructing Empty DataFrame having Both Rows and Columns Using Pandas.DataFrame() Method

We’re going to make an empty Pandas DataFrame object right now and it will have both rows and columns. A pandas DataFrame object with the provided index and columns list is returned once the pd.DataFrame() function of the pandas DataFrame object is invoked using the two arguments of columns and index.

The output is displayed below:


In this article, we have explained the process of constructing an empty DataFrame using the pandas DataFrame function. We have discussed the empty dataframe and provided you with different illustrations to learn about it. The first example explains how to create an empty dataframe without any rows or columns. In the second and third examples, we created an empty dataframe with rows and then columns respectively.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.