大熊猫,融化,未融化保存指数

时间:2018-05-25 12:15:43

标签: python python-2.7 pandas linear-programming

我有一张客户表(coper)和资产分配(资产)

A = [[1,2],[3,4],[5,6]]
idx = ['coper1','coper2','coper3']
cols = ['asset1','asset2']

df = pd.DataFrame(A,index = idx, columns = cols)

所以我的数据看起来像

        asset1  asset2
coper1       1       2
coper2       3       4
coper3       5       6

我希望通过线性优化来运行它们(我有约束 - 像sum of all of asset_i <= amount_on_hand_isum of coper_j = price_j那样的somtehing)

所以我必须将这个2D矩阵变成一维矢量。这很容易融化

df2 = pd.melt(df,value_vars=['asset1','asset2'])

但是现在,当我尝试解开它时,我会得到一个包含大量空白的6行数组!

df2.pivot(columns = 'variable', values = 'value')


variable  asset1  asset2
0            1.0     NaN
1            3.0     NaN
2            5.0     NaN
3            NaN     2.0
4            NaN     4.0
5            NaN     6.0

使用融化时,有没有办法保留索引的'coper'部分?

3 个答案:

答案 0 :(得分:13)

您需要保留reset_index和参数id_vars的索引值:

df2 = pd.melt(df.reset_index(), id_vars='index',value_vars=['asset1','asset2'])
print (df2)
    index variable  value
0  coper1   asset1      1
1  coper2   asset1      3
2  coper3   asset1      5
3  coper1   asset2      2
4  coper2   asset2      4
5  coper3   asset2      6

然后转动工作很好:

print(df2.pivot(index='index',columns = 'variable', values = 'value'))
variable  asset1  asset2
index                   
coper1         1       2
coper2         3       4
coper3         5       6

stack的另一种可能解决方案:

df2 = df.stack().reset_index()
df2.columns = list('abc')
print (df2)
        a       b  c
0  coper1  asset1  1
1  coper1  asset2  2
2  coper2  asset1  3
3  coper2  asset2  4
4  coper3  asset1  5
5  coper3  asset2  6

print(df2.pivot(index='a',columns = 'b', values = 'c'))
b       asset1  asset2
a                     
coper1       1       2
coper2       3       4
coper3       5       6

答案 1 :(得分:3)

看起来像“可选参数keep_index到数据框熔化方法”进入了1.1版:https://github.com/pandas-dev/pandas/issues/17440

答案 2 :(得分:1)

将ignore_index设置为False,以保留索引,例如

df = df.melt(var_name=‘species’, value_name=‘height’, ignore_index = False)