23.1 C
London
Monday, July 21, 2025
HomePandas in PythonDataFrame Functions in PythonPandas: How to Create Empty DataFrame with Column Names

Pandas: How to Create Empty DataFrame with Column Names

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to create an empty pandas DataFrame with specific column names:

df = pd.DataFrame(columns=['Col1', 'Col2', 'Col3'])

The following examples shows how to use this syntax in practice.

Example 1: Create DataFrame with Column Names & No Rows

The following code shows how to create a pandas DataFrame with specific column names and no rows:

import pandas as pd

#create DataFrame
df = pd.DataFrame(columns=['A', 'B', 'C', 'D', 'E'])

#view DataFrame
df

A   B   C   D   E

We can use shape to get the size of the DataFrame:

#display shape of DataFrame
df.shape

(0, 5)

This tells us that the DataFrame has 0 rows and 5 columns.

We can also use list() to get a list of the column names:

#display list of column names
list(df)

['A', 'B', 'C', 'D', 'E']

Example 2: Create DataFrame with Column Names & Specific Number of Rows

The following code shows how to create a pandas DataFrame with specific column names and a specific number of rows:

import pandas as pd

#create DataFrame
df = pd.DataFrame(columns=['A', 'B', 'C', 'D', 'E'],
                  index=range(1, 10))
#view DataFrame
df

        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
8	NaN	NaN	NaN	NaN	NaN
9	NaN	NaN	NaN	NaN	NaN

Notice that every value in the DataFrame is filled with a NaN value.

Once again, we can use shape to get the size of the DataFrame:

#display shape of DataFrame
df.shape

(9, 5)

This tells us that the DataFrame has 9 rows and 5 columns.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Create New Column Based on Condition in Pandas
How to Insert a Column Into a Pandas DataFrame
How to Set Column as Index in Pandas

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories