• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Create Empty DataFrame with pandas

Using Python to Create Empty DataFrame with pandas

August 19, 2022 Leave a Comment

To create an empty DataFrame with pandas in Python, you can use the DataFrame() function.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(empty_dataframe)

#Output:
Empty DataFrame
Columns: []
Index: []

When working with the pandas in Python, the main data structure which is the DataFrame.

Depending on the program you are designing, you might need to create an empty DataFrame.

Creating an empty pandas DataFrame is easy. To create an empty DataFrame with pandas in Python, you can use the DataFrame() function.

Below shows you how to create an empty pandas DataFrame in Python.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(empty_dataframe)

#Output:
Empty DataFrame
Columns: []
Index: []

Properties of Empty pandas DataFrames

There are a number of properties of empty DataFrames which you should be aware of.

First, empty DataFrames do not have an index, have no columns and have no rows.

You can see this when you print an empty DataFrame to the console.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(empty_dataframe)

#Output:
Empty DataFrame
Columns: []
Index: []

Since there are no rows or columns in an empty DataFrame, the size of an empty DataFrame is 0.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(empty_dataframe.size)

#Output:
0

The length of an empty DataFrame is also 0.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(len(empty_dataframe)

#Output:
0

How to Check if pandas DataFrame is Empty

There are a few ways you can check if a DataFrame is empty.

The easiest way to check if a pandas DataFrame is empty is with the empty property.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(empty_dataframe.empty)

#Output:
True

You can also check if a DataFrame is empty by checking if the length is 0 or the length of the index is 0.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(len(empty_dataframe) == 0)
print(len(empty_dataframe.index) == 0)

#Output:
True
True

Concatenating Data to Empty DataFrame in Python

One useful case where you might want to create an empty DataFrame is if you are going to create other DataFrames and then concatenate those DataFrames in a loop.

Let’s say you have a function which loops and creates new DataFrames which you want to append.

You can use the pandas concat() function to concat DataFrames to an empty DataFrame in the following way.

import pandas as pd

df = pd.DataFrame()

for x in range(0,10):
    #steps creating new DataFrame named df_new
    df = pd.concat([df, df_new], ignore_index=True)

Hopefully this article has been useful for you to learn how to create empty DataFrames in Python.

Other Articles You'll Also Like:

  • 1.  pi in Python – Using Math Module and Leibniz Formula to Get Value of pi
  • 2.  Add Seconds to Datetime Variable Using Python timedelta() Function
  • 3.  Get pandas Index Values as List in Python
  • 4.  Count Values by Key in Python Dictionary
  • 5.  Get Days of timedelta Object in Python
  • 6.  Remove Parentheses from String Using Python
  • 7.  Loop Through Files in Directory Using Python
  • 8.  pandas idxmin – Find Index of Minimum Value of Series or DataFrame
  • 9.  Python acosh – Find Hyperbolic Arccosine of Number Using math.acosh()
  • 10.  Does Python Use Semicolons? Why Python Doesn’t Use Semicolons

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy