Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an empty data frame with index from another data frame

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 ?

like image 348
Michal Avatar asked Aug 11 '13 21:08

Michal


People also ask

How do you create an empty DataFrame based on another DataFrame?

First, you need to get the list of columns from the dataframe df using df. columns . Then, you can create an empty dataframe by passing this column list to columns parameter.


1 Answers

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.

like image 196
Viktor Kerkez Avatar answered Sep 27 '22 21:09

Viktor Kerkez