使用两列作为变量的数据框从长到宽
Dataframe long to wide using two columns as variables
我想使用 port1
和 port2
作为感兴趣的变量将以下数据从长到宽重塑
port1 port2 w_ret
date
2006-01-01 0.0 0.0 0.067991
2006-01-01 0.0 1.0 0.033219
2006-01-01 1.0 0.0 0.073324
2006-01-01 1.0 1.0 0.039730
2006-01-02 0.0 0.0 0.033616
2006-01-02 0.0 1.0 0.022452
2006-01-02 1.0 0.0 -0.024854
2006-01-02 1.0 1.0 0.020411
我希望重新排列的数据看起来像这样:
0.00.0 0.01.0 1.00.0 1.01.0
date
2006-01-01 0.067991 0.033219 0.073324 0.039730
2006-01-02 0.033616 0.022452 -0.024854 0.020411
顶部的数字类似于 port1
和 port2
数字。我不确定当使用正确的代码时最终会发生什么。
我曾尝试 unstack()
使用 port1
和 port2
作为索引 date
并尝试使用 pivot_table
,但没有成功实现这个输出。
任何想法都会很棒!
首先将列连接在一起,然后 DataFrame.set_index
with parameter append=True
and last reshape by Series.unstack
:
s = df['port1'].astype(str) + df['port2'].astype(str)
df = df.set_index(s, append=True)['w_ret'].unstack()
print (df)
0.00.0 0.01.0 1.00.0 1.01.0
date
2006-01-01 0.067991 0.033219 0.073324 0.039730
2006-01-02 0.033616 0.022452 -0.024854 0.020411
我想使用 port1
和 port2
作为感兴趣的变量将以下数据从长到宽重塑
port1 port2 w_ret
date
2006-01-01 0.0 0.0 0.067991
2006-01-01 0.0 1.0 0.033219
2006-01-01 1.0 0.0 0.073324
2006-01-01 1.0 1.0 0.039730
2006-01-02 0.0 0.0 0.033616
2006-01-02 0.0 1.0 0.022452
2006-01-02 1.0 0.0 -0.024854
2006-01-02 1.0 1.0 0.020411
我希望重新排列的数据看起来像这样:
0.00.0 0.01.0 1.00.0 1.01.0
date
2006-01-01 0.067991 0.033219 0.073324 0.039730
2006-01-02 0.033616 0.022452 -0.024854 0.020411
顶部的数字类似于 port1
和 port2
数字。我不确定当使用正确的代码时最终会发生什么。
我曾尝试 unstack()
使用 port1
和 port2
作为索引 date
并尝试使用 pivot_table
,但没有成功实现这个输出。
任何想法都会很棒!
首先将列连接在一起,然后 DataFrame.set_index
with parameter append=True
and last reshape by Series.unstack
:
s = df['port1'].astype(str) + df['port2'].astype(str)
df = df.set_index(s, append=True)['w_ret'].unstack()
print (df)
0.00.0 0.01.0 1.00.0 1.01.0
date
2006-01-01 0.067991 0.033219 0.073324 0.039730
2006-01-02 0.033616 0.022452 -0.024854 0.020411