Summary: df2 = pd.DataFrame(index=df1.index)
can be used to to create an empty DataFrame. It will not have any columns but just an index which is the same as in the already given DataFrame, i.e., df1.
Introduction
Problem Statement: How to create an empty data frame with an index from another data frame?
Example: Let’s consider that we have a specific data frame, and we want to create an empty DataFrame using the index of the such that we can add new columns to the empty data frame accordingly.
import pandas as pd # Creating the data frame df1 = pd.DataFrame({ 'A': [5, 10, 15, 20], 'B': ['w', 'x', 'y', 'x'], 'C': [False, True, False, True], }) # SOME METHOD TO CREATE ANOTHER EMPTY DATAFRAME df2 USING INDEX OF df1
Expected Output:
Empty DataFrame
Columns: []
Index: [0, 1, 2, 3]
Quick Recap to DataFrame: Data frame is a two-dimensional data structure that is immutable and heterogeneous. The data structure has labeled axes- rows and columns. Pandas Dataframe comprises three parts: data, rows, and columns. Here’s what a data frame looks like –
A B C
0 5 w False
1 10 x True
2 15 y False
3 20 x True
Recommended Read: How to Create a DataFrame in Pandas?
What Is An Empty DataFrame?
An empty dataFrame is the data frame object that has no data in it, i.e., the length of all the axes is zero. The empty data frame must either have zero number of rows or columns and it needs to have the shape (0, n). The data frame that has rows with None or NaN values is not considered empty. The shape (n, 0) is not considered to be empty as it has n rows.
We can even check if the data frame is empty by using the df.empty()
method of the pandas DataFrame object. The df.empty()
method when applied on a data frame returns a Boolean
value, i.e. True
or False
. It returns True
when the data frame is empty and returns False
when the data frame is not empty.
Now that we know what a data frame and an empty data frame is, let’s create an empty data frame with an index from a given data frame.
Using df.index
To create an empty data frame with an index from another data frame, we have to use the index of the first data frame and assign it to the second (empty) data frame. The method will hence create a dataFrame without any columns. It will consider only the index, and it is the same as the first data frame.
Solution:
# Importing the panda's module import pandas as pd # Creating the data frame df1 = pd.DataFrame({ 'A': [5, 10, 15, 20], 'B': ['w', 'x', 'y', 'x'], 'C': [False, True, False, True], }) print("The first data frame:") print(df1) # Creating an empty dataframe using index df2 = pd.DataFrame(index = df1.index) print(df2)
Output:
The first data frame:
A B C
0 5 w False
1 10 x True
2 15 y False
3 20 x True
Empty DataFrame
Columns: []
Index: [0, 1, 2, 3]
In the above example, as the first data frame (df1) has four indexes, the empty data frame(df2) also has the same four indexes.
Better Approach: Using df.index.copy()
The df.index.copy()
is similar to the df.index
method. However, it is better to set the index as df1.index.copy()
or else both the data frames (df1 and df2) will share the same index object, i.e., if you set the df2.index.name = 'demo'
, the first data frame’s index will also get the same name.
Solution
# Importing the pandas' module import pandas as pd # Creating the data frame df1 = pd.DataFrame({ 'A': [5, 10, 15, 20], 'B': ['w', 'x', 'y', 'x'], 'C': [False, True, False, True], }) print("The first data frame:") print(df1) # Creating an empty dataframe using index df2 = pd.DataFrame(index = df1.index.copy()) print(df2)
Output:
The first data frame:
A B C
0 5 w False
1 10 x True
2 15 y False
3 20 x True
Empty DataFrame
Columns: []
Index: [0, 1, 2, 3]
Assigning The Index Directly
We can assign the index of the first data frame (df1) to the second data frame (df2) directly. The idea here is to define a new DataFrame and directly assign index from the already given data frame to the newly created empty data frame.
Example:
# Importing the pandas' module import pandas as pd # Creating the data frame df1 = pd.DataFrame({ 'A': [5, 10, 15, 20], 'B': ['w', 'x', 'y', 'x'], 'C': [False, True, False, True], }) print("The first data frame:") print(df1) # Creating an empty dataframe using index df2 = pd.DataFrame() df2.index = df1.index print(df2)
Output:
The first data frame:
A B C
0 5 w False
1 10 x True
2 15 y False
3 20 x True
Empty DataFrame
Columns: []
Index: [0, 1, 2, 3]
Caution: You must ensure that the lengths of the indices are the same, or else we may get the ValueError
.
Using copy()
If you wish to create a deep copy using the index of a given data frame and store it in an empty data frame then using the copy(deep = True)
method is a good option. This denotes that any modifications made to df1 will not be reflected in df2 and vice versa as they point at different objects.
Look at the following example to understand how to create an empty data frame with an index from another data frame.
Example:
# Importing the pandas' module import pandas as pd # Creating the data frame df1 = pd.DataFrame({ 'A': [5, 10, 15, 20], 'B': ['w', 'x', 'y', 'x'], 'C': [False, True, False, True], }) print("The first data frame:") print(df1) # Creating an empty dataframe using index df2 = df1[[]].copy() print(df2)
Output:
The first data frame:
A B C
0 5 w False
1 10 x True
2 15 y False
3 20 x True
Empty DataFrame
Columns: []
Index: [0, 1, 2, 3]
Conclusion
In this article, we looked at the different methods to create an empty data frame with an index from another data frame. I hope you found it helpful. Please stay tuned and subscribe for more such articles.
Recommended Read: How to Select Rows From a DataFrame Based on Column Values
Article By: Shubham Sayon and Rashi Agarwal
Learn Pandas the Fun Way by Solving Code Puzzles
If you want to boost your Pandas skills, consider checking out my puzzle-based learning book Coffee Break Pandas (Amazon Link).
It contains 74 hand-crafted Pandas puzzles including explanations. By solving each puzzle, you’ll get a score representing your skill level in Pandas. Can you become a Pandas Grandmaster?
Coffee Break Pandas offers a fun-based approach to data science mastery—and a truly gamified learning experience.