How to copy data from one dataframe to another without altering column and rownames in pandas

4090 views python
2

I have the below table

  A B C
D 1 2 3
E 4 5 6
F 6 7 8

After fit_transform(), and converting back to dataframe, I get the below table

 0   1   2
0 v1  v2 v3
1 v4  v5 v5
2 v6  v7 v8

v1-v8 some normalised values.

But I want to retain my column and row names while doing fit_transform/after converting to dataframes. I'm not getting how to do that. Please help.

answered question

Give the columns and index from your initial dataframe to the dataframe constructor when you convert back

1 Answer

8

Try assigning column and index of the original df to the new one by:

out_df.columns = df.columns
out_df.index = df.index

df is the original dataframe; out_df is the dataframe after fit_transform()

Note : This will work if the cell order of the dataframe i.e. rows and column are in the same order as that of original df

posted this

Have an answer?

JD

Please login first before posting an answer.