How to Create Empty Pandas DataFrame with Column Names?

Creating an empty Pandas DataFrame with column names can be done by providing the column names as a list to the DataFrame constructor. The DataFrame will be empty, but it will contain the specified columns, each of which will have a default data type assigned to it. We can then populate the DataFrame with data by adding rows to it one at a time.


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)

x