120

I've got a data frame df1 with multiple columns and rows. Simple example:

    TIME T1  T2 
       1 10 100
       2 20 200
       3 30 300

I'd like to create an empty data frame df2 and later on, add new columns with the calculation results.

For this moment my code looks like this:

     df1=pd.read_csv("1.txt",index_col="TIME")

     df2=df1.copy()[[]] #copy df1 and erase all columns

...adding two new columns:

     df2["results1"],df2["results2"]=df1["T1"]*df["T2"]*3,df1["T2"]+100

Is there any better/safer/faster way to do this ? Is it possible to create an empty data frame df2 and only copy index from df1 ?

1
  • I like your original suggestion, except you don't have to copy and erase the whole dataframe: df2=df1[[]].copy() #empty slice, then copy Commented Mar 18, 2019 at 19:45

5 Answers 5

193
df2 = pd.DataFrame(index=df1.index)

This will create a DataFrame with no columns but just an index, and it will be the same index as in the df1.

Sign up to request clarification or add additional context in comments.

4 Comments

It's better set index as df1.index.copy(); otherwise, df1 and df2 will share the same index object
Indexes are immutable exactly for this reason.
While you set df2.index.name = 'test', df1's index will also get the name.
The bug will be fixed in the next version, so it's better use df1.index.copy() before 0.13 released
15

It's better to set index as df1.index.copy()

df2 = pd.DataFrame(index=df1.index.copy())

You can use df1.index is df2.index to check whether they are the same object

1 Comment

It's not, you just make an unnecessary copy of an index. Indexes are immutable exactly for this reason - to be able to share them among different data structures without a fear that they gonna change.
11

You can also assign the index of a dataframe to another dataframe directly.

df2.index=df1.index

1 Comment

The lengths of the indices must match though, otherwise you get a ValueError for Length mismatch.
4

You can use this short code:

df2=df1[[]].copy()

Comments

1

To avoid geting all the NaN after the concat add the index to it.

df1 = pd.DataFrame(x1.toarray(),index=simpledf.index, columns=v.get_feature_names())

When defining the new dataframe with X transformed use the same index as the original dataframe.

1 Comment

This is a very good reminder!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.