What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Create Empty Dataframe in Pandas

  • Nov 26, 2021
  • 5 Minutes Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Adrita Das
Create Empty Dataframe in Pandas

 

DataFrame is a container class that stores and manipulates two-dimensional data organized in tabular format within the python data analysis library 'Pandas'. The data, rows, and columns are the three main components of a Pandas DataFrame. You can store different types of elements in each column of DataFrame. For instance, one column of a DataFrame can have all its elements as integers, while another column can have all its elements as string literals. However, you can make use of the attribute name 'dtype' and return the object type of the column. 
Oftentimes, to save memory, we may want to build an empty DataFrame. If you simply want to add records with two values rather than the entire DataFrame, for example, you can construct an empty DataFrame first and then append the values one by one. In this article, you will learn how to create an empty DataFrame in python using pandas. We'll also talk about how to fill it with data later by adding rows or columns. So, let’s get started!

How to Create Empty DataFrame in Pandas?

Below are the three methods by which you can create an empty DataFrame in pandas:

1) Creating a dataframe without rows and columns

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. Note that the DataFrame() class available in the pandas library is similar to the constructor which is used to construct the class.

For example:

# import pandas library as pd
import pandas as pd
 
# create an Empty DataFrame object
df = pd.DataFrame()
 
print(df)
 
# append columns to an empty DataFrame
df['Name'] = ['Anna', 'Pete', 'Tommy']
df['Scores'] = [97, 600, 200]
df['Questions'] = [2200, 75, 100]
 
df

 

Output: 

       Name	Scores  Questions
0	Anna	97	2200
1	Pete	600	75
2	Tommy	200	100

 

2) Create an empty dataframe with only columns 

The second method is to create an empty dataframe with only columns in it. Later, to complete the DataFrame and add data into it, you can create and append the rows using the in-built append() method as shown in the below example. 

For example:

# import pandas library as pd
import pandas as pd
 
# create an Empty DataFrame
# object With column names only
df = pd.DataFrame(columns = ['Name', 'Scores', 'Questions'])
print(df)
 
# append rows to an empty DataFrame
df = df.append({'Name' : 'Anna', 'Scores' : 97, 'Questions' : 2200}, 
                ignore_index = True)
df = df.append({'Name' : 'Linda', 'Scores' : 30, 'Questions' : 50},
                ignore_index = True)
df = df.append({'Name' : 'Tommy', 'Scores' : 17, 'Questions' : 220},
               ignore_index = True)
df

 

Output: 

Empty DataFrame
Columns: [Name, Scoress, Questions]
Index: []
        Name Scores	Questions
0	Anna	97	2200
1	Linda	30	50
2	Tommy	17	220

 

3) Create an empty dataframe with column name and indices

Another method is to create the empty dataframe using columns and indices in it. As the indices are passed while creating the DataFrame, you can easily append the rows using the loc() function. It helps to retrieve data values from a dataset that are fitted in particular rows and columns based on index value passed. Check out the below example for a better understanding. 

For example:

# import pandas library as pd
import pandas as pd
 
# create an Empty DataFrame object With
# column names and indices 
df = pd.DataFrame(columns = ['Name', 'Scores', 'Questions'], 
                   index = ['a', 'b', 'c'])
 
print("Empty DataFrame With NaN values : \n\n", df)
 
# adding rows to an empty 
# dataframe at existing index
df.loc['a'] = ['Anna', 50, 100]
df.loc['b'] = ['Pete', 60, 120]
df.loc['c'] = ['Tommy', 30, 60]
 
df

 

Output:

Empty DataFrame With NaN values : 

   Name    Scores Questions
a  NaN      NaN      NaN
b  NaN      NaN      NaN
c  NaN      NaN      NaN
        Name	Scores  Questions
a	Anna	50	100
b	Pete	60	120
c	Tommy	30	60

 

Conclusion

Data Analysis is one of the crucial steps for any programmer in the technical world. Also, many large companies make use of the huge data received from their customers and draw valuable insights from it. In this article, we studied how to create an empty DataFrame and store this large amount of data into it by appending rows and columns. All these methods are simple and highly recommended for making your coding efficient. 

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Adrita Das
I am Adrita Das, a Biomedical Engineering student and technical content writer with a background in Computer Science. I am pursuing this goal through a career in life science research, augmented with computational techniques. I am passionate about technology and it gives immense happiness to share my knowledge through the content I make.