In this article we will discuss different ways to create an empty DataFrame and then fill data in it later by either adding rows or columns.

Suppose we want to create an empty DataFrame first and then append data into it at later stages. Let’s see how to do that,

Import python’s pandas module like this,

Create an empty DataFrame with only column names but no rows

Suppose we know the column names of our DataFrame but we don’t have any data as of now. So we will create an empty DataFrame with only column names like this,

Contents of the created empty DataFrame will be,

How it worked ?

Dataframe class provides a constructor to create Dataframe object by passing column names , index names & data in argument like this,

To create an empty dataframe object we passed columns argument only and for index & data default arguments will be used.

Append rows to empty DataFrame

As we have created an empty DataFrame, so let’s see how to add rows to it,

Output:

Three rows were added to the DataFrame.

Create an complete empty DataFrame without any column name or indices

We can create a complete empty dataframe by just calling the Dataframe class constructor without any arguments like this,

As we have not passed any arguments, so default value of all arguments will be None and it will create an empty dataframe dfObj. It’s contents are as follows,

Now let’s see how to append columns with data to this empty Dataframe,

Appends columns to an empty DataFrame

Output:

Create an empty Dataframe with column names & row indices but no data

It might be possible in some cases that we know the column names & row indices at start but we don’t have data yet. So we will create an empty DataFrame and add data to it at later stages like this,

Here we passed the columns & index arguments to Dataframe constructor but without data argument. So, it will create an empty dataframe with all data as NaN.

Add rows to an empty dataframe at existing index

output:

Complete example is as follows:

Output:

 

 

Click Here to Subscribe for more Articles / Tutorials like this.