简体   繁体   English

如何在numpy数组转换后恢复pandas DataFrame rownames和列名

[英]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: 然后我转换为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 . 我的问题是如何从df恢复列和行名称( gn )到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

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

By using .loc assign the value 通过使用.loc分配值

df.loc[:,['x','y']]=ary
df
Out[849]: 
    gn    x    y
0  foo  2.1  1.1
1  bar  3.1  4.4
2  qux  1.5  8.0

more info 更多信息

ary=np.array([[ 2.1,  1.1],
       [ 3.1,  4.4],
       [ 1.5,  8. ]])

Since you have more column 由于你有更多的专栏

df.loc[:,list(df.set_index("gn"))]=ary

I am having trouble understanding why you would do the array conversion in the first place. 我无法理解你为什么要首先进行数组转换。 Is that a mandate? 这是一项授权吗? If not, here's a pure-play pandas version that would do all operations at one go - 如果没有,这里是一个纯粹的玩熊猫版本,可以一次完成所有操作 -

df = df + 1

So, full code will be - 所以,完整的代码将是 -

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 = df + 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM