Q:

How to recover back pandas DataFrame rownames and column names after numpy array transformation

I have the following data frame:
import numpy as np
import pandas as pd

x = [1.1,2.1,0.5]
y = [0.1,3.4,7]
gn = ['foo','bar','qux']
df = pd.DataFrame({'gn':gn, 'x':x, 'y':y})
df

which produces this:
In [148]: df
Out[148]:
    gn    x    y
0  foo  1.1  0.1
1  bar  2.1  3.4
2  qux  0.5  7.0

Then I do some transformation after converting to numpy ndarray:
df.set_index("gn",inplace=True)
npar = df.as_matrix()
npar_new = npar + 1
npar_new

Which produces this: 
array([[ 2.1,  1.1],
       [ 3.1,  4.4],
       [ 1.5,  8. ]])

My question is how can I recover the column and row name (gn) from df into npar_new. The desired final result is:
   gn    x    y
  foo  2.1  1.1
  bar  3.1  4.4
  qux  1.5  8.0

A:

You can try
df_new = pd.DataFrame(npar_new, index = df.index, columns = df.columns)

    x   y
gn      
foo 2.1 1.1
bar 3.1 4.4
qux 1.5 8.0

