我手动完成了以下任务,我确信有一种方法可以编写循环,但我不确定如何在Python中执行此操作。
数据如下所示:
DF
a b c market ret
date id
2015-01-01 1 10 4 2 10 0.02
2015-01-01 2 20 3 5 15 0.03
2015-01-01 3 30 2 3 20 0.05
2015-01-01 4 40 1 10 25 0.01
2015-01-02 1 15 8 4 15 -0.03
2015-01-02 2 10 6 1 10 0.02
2015-01-02 3 25 10 2 22 0.06
2015-01-02 4 30 3 7 26 0.06
2015-01-03 1 25 2 2 16 -0.07
2015-01-03 2 10 6 1 18 0.01
2015-01-03 3 5 8 5 26 0.04
2015-01-03 4 30 1 6 21 -0.05
我做以下事情:
dfa = df
dfa['market'] = dfa.groupby(level = ['id']).market.shift()
dfa['port'] = dfa.groupby(['date'])['a'].transform(lambda x: pd.qcut(x, 4, labels = False))
# value-weighted portoflio returns
dfa = dfa.set_index(['port'], append = True)
dfa['tmktcap'] = dfa.groupby(['date','port'])['mktcap'].transform(sum)
dfa['w_ret'] = (dfa.mktcap / dfa.tmktcap) * dfa.ret
#reshape long to wide
dfa = dfa.groupby(['date', 'port'])['w_ret'].sum().shift(-4)
dfa = dfa['2006-01-01':].rename('a')
dfa = dfa.unstack()
dfa[4.0] = dfa[3.0] - dfroe[0.0]
dfa = dfa.stack().reset_index().set_index(['date'])
dfa['port'] = dfa['port'].map({0.0:'a0',1.0:'a1',2.0:'a2',3.0:'a3',4.0:'aL-S'})
dfa = dfa.reset_index().set_index(['date', 'port']).unstack()
但后来我为b和c重复了这个任务。
因此,我首先设置dfb = df
,然后将a
更改为b
,并在为c
执行此操作时执行此过程。
对于从a
到h
的变量,我不得不这样做(这里只是一些示例数据),所以编写循环的任何帮助都会很棒!!!!!