Register Login

How to create an empty DataFrame in python

Updated Mar 05, 2022

DataFrames are one of the popular two-dimensional data structures in Pandas. It provides a table-like structure of the data with rows and columns. Some software requires empty DataFrames so that users can start filling the data in that data structure. In this article, we will learn how to create an empty DataFrame using Python Pandas.

What are DataFrames in pandas?

Pandas DataFrames are 2D tabular shaped, heterogeneous, mutable, multi-functional data structures of Pandas module that have labeled axes (called rows & columns). Usually, we use list, tuple dictionary, or NumPy Arrays to create it. In this data structure, all the data remains aligned in a tabular format. A DataFrame consists of 3 principal components. These are row, column, and data. Programmers have to use the pandas.DataFrame() to create a DataFrame.

Example:

import pandas as pd
dat = ['Gaurav', 'Ray', 'Karlos', 'Mandes']
df2 = pd.DataFrame(dat)
print(df2)

Output:

How to create an empty DataFrame:

Programmers can do multiple tasks by using an empty DataFrame. It can help a data science app store fresh data on the app. Others use this technique to reset the DataFrame's data. Let us now take a look at the different ways through which a Python programmer can create an empty DataFrame using Pandas.

Method 1: Creating DataFrames without column name and indices:

The easiest way to create an empty DataFrame is to simply call it with pandas.DataFrame() and not passing any other object within it.

Example:

import pandas as pd
df2 = pd.DataFrame()
print(df2)

Output:

Method 2: Creating DataFrames with Column Mentions:

Another way of creating an empty DataFrame is by passing the strings as parameter within the DataFrame() method that will be accepted as Column heading names but such method does not cater to any value within the DataFrame.

Example:

import pandas as pd
df2 = pd.DataFrame(columns = ['Emp_Name', 'EBooks_Published', 'Edition Number'])
print(df2)

Output:

Method 3: Create an empty DataFrame using a column name and providing indices:

This is another method where the DataFrame gets created without value (this time with NaN, Not a Number) and is having column heading and row index mentioned explicitly using columns and index parametric values.

Example:

import pandas as pd
df2 = pd.DataFrame(columns = ['EmpName', 'EBooks', 'Ed'], 
                   index = ['a', 'b', 'c'])

Output:

Let us now take a look at 2 programs (in two different techniques) where programmers can use two different approaches to append values as records into the DataFrame.

Example of adding data records using dataframeObject.loc:

import pandas as pd
df2 = pd.DataFrame(columns = ['EmpName', 'EBooks', 'Ed'], 
                   index = ['a', 'b', 'c'])
df2.loc['a'] = ['Karlos', 'Data Privacy', 1]
df2.loc['b'] = ['Ray', 'Network Security', 4]
df2.loc['c'] = ['Su', 'Game Development', 3]
print(df2)

Output:

Example of adding data records using append() method.

import pandas as pd
df = pd.DataFrame(columns = ['Name', 'ResearchPaper', 'Improved'])
print(df)
df = df.append({'Name' : 'Gaurav', 'ResearchPaper' : 18, 'Improved' : 12}, ignore_index = True)
df = df.append({'Name' : 'Ray', 'ResearchPaper' : 35, 'Improved' : 5}, ignore_index = True)
df = df.append({'Name' : 'Dee', 'ResearchPaper' : 9, 'Improved' : 14}, ignore_index = True)
df

Output:

Conclusion:

We hope this article has given you a clear idea of the different ways of creating empty DataFrame and where it can be used. Also, this article talks about the different approaches of adding data records in the DataFrame. If programmer do not put columns and index, then by default the DataFrame considers it from 0 1 2… and so on. But, it is always a good practice to mention the column names and index values for the DataFrames, even when it is empty.


×