【发布时间】:2018-11-04 20:13:40
【问题描述】:
我有一张客户表(铜)和资产配置(资产)
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_i 和sum of coper_j = price_j)
所以我必须把这个二维矩阵变成一维向量。哪个容易融化
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
有什么方法可以在使用熔体时保留我的索引的“铜”部分?
【问题讨论】:
标签: python python-2.7 pandas linear-programming