• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Create Empty Dataframe in Python

Create Empty Dataframe in Python

Author: Aditya Raj
Last Updated: March 1, 2023

Pandas dataframes are used to process tabular data in python. Sometimes we need to create an empty dataframe to fill in values later. This article discusses how to create empty pandas dataframe in python. 

Table of Contents
  1. Create an Empty Pandas DataFrame Without Rows or Columns
  2. Pandas Empty DataFrame With Column Names
  3. Empty DataFrame With Column Names and Row Indices
  4. Conclusion

Create an Empty Pandas DataFrame Without Rows or Columns

To create a dataframe without rows or columns, we can use the DataFrame() function defined in the pandas module without any input arguments. After execution, the DataFrame() function returns an empty dataframe. You can observe this in the following example.

import pandas as pd
df=pd.DataFrame()
print("The empty dataframe is:")
print(df)

Output:

The empty dataframe is:
Empty DataFrame
Columns: []
Index: []

In the above output, you can observe that the dataframe contains no columns or indices.

Pandas Empty DataFrame With Column Names

Instead of creating a completely empty dataframe, you can create a dataframe with column names. For this, we will pass the column names to the columns parameter in the DataFrame() function. The DataFrame() function takes a list of column names as an input argument to the columns parameter. After execution, it returns an empty dataframe with only column names. You can observe this in the following example.

import pandas as pd
column_names=["A","B","C","D","E"]
df=pd.DataFrame(columns=column_names)
print("The empty dataframe is:")
print(df)

Output:

The empty dataframe is:
Empty DataFrame
Columns: [A, B, C, D, E]
Index: []

In this example, we have created a dataframe with columns A, B, C, D, and E. For this, we passed a list containing the column names to the columns parameter in the DataFrame() function.

Empty DataFrame With Column Names and Row Indices

With column names, you can also add indices to the rows of an empty dataframe. To create a dataframe with column names and indices, we will pass the list containing column names to the columns parameter and the list containing indices to the index parameter of the DataFrame() function. After execution of the DataFrame() function, we will get an empty dataframe with specified column names and indices as shown below.

import pandas as pd
column_names=["A","B","C","D","E"]
indices=[1,2,3,4,5,6,7]
df=pd.DataFrame(columns=column_names,index=indices)
print("The empty dataframe is:")
print(df)

Output:

The empty dataframe is:
     A    B    C    D    E
1  NaN  NaN  NaN  NaN  NaN
2  NaN  NaN  NaN  NaN  NaN
3  NaN  NaN  NaN  NaN  NaN
4  NaN  NaN  NaN  NaN  NaN
5  NaN  NaN  NaN  NaN  NaN
6  NaN  NaN  NaN  NaN  NaN
7  NaN  NaN  NaN  NaN  NaN

In the above example, you can observe that we have created an empty dataframe with given column names and row indices. Here, the empty values in the dataframe are shown as NaN values.

Conclusion

In this article, we discussed how to create empty pandas dataframe in python. We also discussed how to create a dataframe with column names and indices. To learn more about dataframes, you can read this article on how to replace nan with 0 in pandas. You might also like this article on how to compare dataframes in python.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us