我有一个这样的数据框
VOW3.DE.csv Rendite VOW3.DE.csv BAS.DE.csv Rendite BAS.DE.csv
0 119.744743 NaN 67.001991 NaN
1 118.470367 -0.010642 65.659218 -0.020041
2 115.557533 -0.024587 65.146057 -0.007816
3 114.374207 -0.010240 62.323662 -0.043324
4 115.512032 0.009948 61.887474 -0.006999
我想得到一个这样的数据框:
Datum | Kurs BAS.DE || Rendite BAS.DE ||| Kurs VOW3.DE |||| Rendite VOW3.DE ||||| Rendite PF
0 2015-11-30 | 67.001991 | NaN | 119.744743 | NaN | NaN
1 2015-12-01 | 65.659218 | -0.020041 | 118.470367 | -0.010642 | -0.015342
2 2015-12-02 | 65.146057 | -0.007816 | 115.557533 | -0.024587 | -0.016201
3 2015-12-03 | 62.323662 | -0.043324 | 114.374207 | -0.010240 | -0.026782
4 2015-12-04 | 61.887474 | -0.006999 | 115.512032 | 0.009948 | 0.001475
我在上面通过手动添加新的列(准确地说是2个)来创建,但是我想使它适用于n只股票以创建包含n个项目的投资组合(第一个数据框)。 因此,在第一个数据帧中,缺少2只股票的回报(伦铁矿)。
第一个数据帧的源代码为:
kurschart = pd.DataFrame()
zaehler = 0
for i in kurse:
x = dateinamen[zaehler]
kurschart[x] = i['Adj Close']
kurschart['Rendite ' + str(x)] = (i['Adj Close'] - i['Adj Close'].shift()) / i['Adj Close'].shift()
rendite = kurschart['Rendite ' + str(x)].values.tolist()
zaehler += 1
和第二个数据框:
kurse = pd.DataFrame()
kurse['Datum'] = kurs1['Date']
kurse['|'] = '|'
kurse['Kurs ' + str((datensatz1))] = kurs1['Adj Close']
kurse['||'] = '|'
kurse['Rendite ' + str(datensatz1)] = (kurs1['Adj Close'] - kurs1['Adj Close'].shift()) / kurs1['Adj Close'].shift()
kurse['|||'] = '|'
kurse['Kurs ' + str((datensatz2))] = kurs2['Adj Close']
kurse['||||'] = '|'
kurse['Rendite ' + str(datensatz2)] = (kurs2['Adj Close'] - kurs2['Adj Close'].shift()) / kurs2['Adj Close'].shift()
kurse['|||||'] = '|'
kurse['Rendite PF'] = (0.5*kurse['Rendite ' + str(datensatz1)] + 0.5 * kurse['Rendite ' + str(datensatz2)])
所以问题是:如何在第一个数据框中添加投资组合收益? 我必须总结n个股票的每个“ Rendite”列。因此,PF返回值应该是列2 + 4 + 6 + 8的总和。。。
答案 0 :(得分:0)
从系数0.5开始,我想您是对算术平均值进行平均。您可以按照以下步骤进行操作:
rendite_columns= [col for col in kurse.columns if col.startswith('Rendite') and col != 'Gesamtrendite']
kurse['Gesamtrendite']= kurse[rendite_columns].mean(axis='columns')
kurse
如果您对以下测试数据执行该操作:
data={
'VOW3.csv' : [23.057553775123335, 23.593044112303037, 23.65484534115204, 23.562766750026583, 23.679798801847944, 23.52374584602828, 23.150216969503184, 23.3361437452337, 23.388247323798225, 23.321390778113823, 23.326745728604624, 23.56376780095595, 23.090108993431173, 23.654695834359266, 23.429795013474937],
'Rendite VOW3.csv': [0.21896520177259293, -0.7427697192703366, 0.6314581124760463, 0.7105503585161574, 0.40627816900027436, -0.057483986769920614, 0.6348684831677514, -0.8766861274013196, -0.9199172014221331, 0.12368836337974098, -0.5354618896920371, -0.7110434003907291, -0.3925526960921031, -0.4946307486439221, 0.5699694168064753],
'BAS.csv' : [100.58494932829151, 100.68866929618761, 99.08030807538707, 99.19352216849971, 99.20636632155544, 98.69847759257573, 99.3536503492825, 100.22568417058993, 100.55155767608073, 100.78163591242063, 99.64791307144067, 98.42429257578625, 98.90368844005607, 99.54198366062207, 99.75836893877648],
'Rendite BAS.csv' : [0.39819439654808453, -0.10365965592851722, -0.1288026640706903, 0.6649485991269095, -0.282756962559618, -0.5769905712495891, 0.17450614250541685, 0.6658463667837213, -0.6711373197537447, -0.6467039920121982, -0.7114906661624425, -0.41867576938838913, -0.4206224095136968, -0.29868088091802614, 0.9159772705246394],
'SIX3.csv' : [102.28099804227904, 103.65989360491852, 102.09607166151251, 102.5775753733126, 103.44819257377404, 103.9918625735876, 101.58953871869825, 103.63134997666835, 102.04822845973479, 103.06169857452153, 103.95049370357287, 102.2882571915577, 103.73419650950392, 103.6273399276302, 103.48925054261484],
'Rendite SIX3.csv' : [0.37056184193995145, -0.4073862599715692, 0.025754571308837404, -0.22521001441204747, 0.17553750395866596, -0.7200706828476542, -0.45796879343182906, 0.457424805756975, -0.40031872763107645, -0.2768806203855809, -0.027986844724395166, 0.6748783172774078, 0.03627400188972252, -0.7014811630547062, -0.9274740387030465],
}
kurse= pd.DataFrame(data)
您得到:
Out[130]:
VOW3.csv Rendite VOW3.csv BAS.csv Rendite BAS.csv SIX3.csv Rendite SIX3.csv Gesamtrendite
0 23.057554 0.218965 100.584949 0.398194 102.280998 0.370562 0.987721
1 23.593044 -0.742770 100.688669 -0.103660 103.659894 -0.407386 -1.253816
2 23.654845 0.631458 99.080308 -0.128803 102.096072 0.025755 0.528410
3 23.562767 0.710550 99.193522 0.664949 102.577575 -0.225210 1.150289
4 23.679799 0.406278 99.206366 -0.282757 103.448193 0.175538 0.299059
5 23.523746 -0.057484 98.698478 -0.576991 103.991863 -0.720071 -1.354545
6 23.150217 0.634868 99.353650 0.174506 101.589539 -0.457969 0.351406
7 23.336144 -0.876686 100.225684 0.665846 103.631350 0.457425 0.246585
8 23.388247 -0.919917 100.551558 -0.671137 102.048228 -0.400319 -1.991373
9 23.321391 0.123688 100.781636 -0.646704 103.061699 -0.276881 -0.799896
10 23.326746 -0.535462 99.647913 -0.711491 103.950494 -0.027987 -1.274939
11 23.563768 -0.711043 98.424293 -0.418676 102.288257 0.674878 -0.454841
12 23.090109 -0.392553 98.903688 -0.420622 103.734197 0.036274 -0.776901
13 23.654696 -0.494631 99.541984 -0.298681 103.627340 -0.701481 -1.494793
14 23.429795 0.569969 99.758369 0.915977 103.489251 -0.927474 0.558473