简体   繁体   English

我怎样才能总结熊猫数据帧中每 x 列和 x+2 列的列?

[英]How can I sum up colum every x-th and x+2-th column in pandas dataframe?

I have an dataframe like this我有一个这样的数据框

   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

and I want to get a dataframe like this:我想得到一个这样的数据框:

         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

I created above by manually adding new colums (2 acctually) but I want to make this working for n stocks to create a portfolio with n items (first dataframe).我通过手动添加新列(实际上是 2 个)来创建上面的,但我想让它适用于 n 个股票以创建一个包含 n 个项目(第一个数据框)的投资组合。 So in the first dataframe the return (rendite) of the 2 stocks is missing.因此,在第一个数据框中,缺少 2 只股票的回报 (rendite)。

The source-code for the first dataframe is:第一个数据帧的源代码是:

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

and for the secound dataframe:对于第二个数据框:

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)])

So the question is: How I can add the portfolio-return in the first dataframe?所以问题是:如何在第一个数据框中添加投资组合回报? I have to sum up every "Rendite"-colum for n stocks.我必须总结 n 个股票的每个“Rendite”列。 So the PF-return should be the sum of colum 2 + 4 + 6 + 8, ...所以PF-return应该是colum 2 + 4 + 6 + 8的总和,......

From the factor 0.5 I suppose you are averaging the returns arithmetically.从因子 0.5 我想你是在算术平均回报。 You can do that as follows:你可以这样做:

rendite_columns= [col for col in kurse.columns if col.startswith('Rendite') and col != 'Gesamtrendite']
kurse['Gesamtrendite']= kurse[rendite_columns].mean(axis='columns')
kurse

If you execute that on the following test data:如果您对以下测试数据执行该操作:

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)

you get:你得到:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从类中的第x个函数引用变量 - Refer to variable from a x-th function within a class 如何每第 4 行将 append 列总计到 pandas dataframe? - How to append column total to pandas dataframe every 4th row? 使用 Pandas DataFrame,如何保留每 7 行? - With a pandas DataFrame, how do I keep every 7th row? 如果给定一个整数列表和一个名为 x 的数字,如何递归返回列表中每个第 x 个数字的总和 - How to if given a list of integers and a number called x, return recursively the sum of every x'th number in the list 解释 pythonic table[n:m] 其中 m 不表示第 x 个元素 - Explain pythonic table[n:m] where m does not mean x-th element 给定文件中的anxm矩阵,如何读取第一列中的每一行? - Given a n x m matrix in a file, how to read every a'th line in the first column? 如何在Pandas Dataframe Python中按列每3行求和 - How to sum up every 3 rows by column in Pandas Dataframe Python Python pandas dataframe 如何将 Z78E6221F6393D1356681DB398FtrCE4 作为第 6 行和第 6 行 - Python pandas dataframe how to output th as a column and tr as the row 隐藏xticks标记每个第n个标签或Pandas图上的值/使x轴可读 - Hiding xticks labels every n-th label or on value on Pandas plot / make x-axis readable 当第 n 列值为“x”时,访问 Pyspark 数据帧的第 (n+1) 列 - Access Pyspark dataframe's (n+1)th column when nth column value is 'x'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM