Create empty Dataframe with same dimensions as another?

Question:

I have searched a bit, but can not find a good answer. I want to create an empty dataframe with same dimensions as another dataframe so I can add new columns. Today I create an empty dataframe filled with zeroes, and then I delete the zero column. I hope there is a better way, but can not find the answer. Can someone help me?

I do like this today and it works, but it is very ugly.

df_copy = pandas.DataFrame(numpy.zeros(len(df_original.index))) 
df_copy = df_copy.drop([0],axis=1) 

And now I can add new columns as I process data. So basically I want an empty dataframe with same dimensions as another dataframe.

df_copy["price"] = pricesList
df_copy["size"] = sizesList

EDIT: Another closely related question: how do I create an empty Dataframe with dimensions mxn? I have got the answer below how to create an empty dataframe with dimensions 1xn, which is by setting the index. But how do I create an empty nxm dataframe filled with zeroes? The reason I am asking, is because I suspect(?) it is faster to create a zero filled dataframe, and then replace each element as needed. The alternative is to create an empty dataframe with dimensions 1xn and then add columns as needed – which I am told is slow. So it might be faster to create an empty dataframe with nxm dimensions and then replace elements as needed (by copying a list to each column). Say a column has 100 rows, and I create a sublist with 25 rows, so I just copy this list to the correct subcolumn, and repeat. This is faster than adding a new column?

Asked By: Orvar Korvar

||

Answers:

import pandas as pd 
df_copy = pd.DataFrame(index=df_original.index,columns=df_original.columns)
Answered By: Gaspare Bonventre

Creating an empty dataframe with the same index and columns as another dataframe:

import pandas as pd
df_copy = pd.DataFrame().reindex_like(df_original)
Answered By: kadee

@GaspareBonventre’s answer can be slow, because of an issue with the Pandas DataFrame constructor. I find it much faster to do

import numpy as np
df_copy = pd.DataFrame(np.zeros(df_original.shape))
df_copy.index = df_original.index
df_copy.columns = df_original.columns
Answered By: Milind R

For anyone coming to this page looking to create a dataframe of same columns, same dtypes, and no rows:

import pandas as pd
df_copy = df_original.iloc[:0,:].copy()
Answered By: Ben Saunders

This one just keeps columns for you 🙂

empty_df = df.iloc[:0]
Answered By: Yas
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.