Reputation: 1173
I have the following DataFrame:
AAPL F IBM
Date
2016-05-02 93.073328 13.62 143.881476
2016-05-03 94.604009 13.43 142.752373
2016-05-04 93.620002 13.31 142.871221
2016-05-05 93.239998 13.32 145.070003
2016-05-06 92.720001 13.44 147.289993
And I have a list of weights: say... w =[20, 30, 50]
I'd like to divide each column by the value of its first row, and then multiply by the corresponding weight.
AAPL F IBM
2016-05-02 93.07/93.07*20 13.62/13.62*30 143.88/143.88*50
2016-05-03 94.60/93.07*20 13.43/13.62*30 142.75/143.88*50
Is there an easy way to do this? and so on
Upvotes: 2
Views: 1831
Reputation: 210832
another way:
w = [20, 30, 50]
In [110]: df /= df.iloc[0]/w
In [111]: df
Out[111]:
AAPL F IBM
Date
2016-05-02 20.000000 30.000000 50.000000
2016-05-03 20.328919 29.581498 49.607627
2016-05-04 20.117472 29.317181 49.648928
2016-05-05 20.035815 29.339207 50.413023
2016-05-06 19.924076 29.603524 51.184488
or like this (depending on what do you want to achieve):
In [103]: df /= (df.iloc[0]*w)
In [104]: df
Out[104]:
AAPL F IBM
Date
2016-05-02 0.050000 0.033333 0.020000
2016-05-03 0.050822 0.032868 0.019843
2016-05-04 0.050294 0.032575 0.019860
2016-05-05 0.050090 0.032599 0.020165
2016-05-06 0.049810 0.032893 0.020474
Upvotes: 4
Reputation: 294258
from StringIO import StringIO
import pandas as pd
import numpy as np
text = """Date AAPL F IBM
2016-05-02 93.073328 13.62 143.881476
2016-05-03 94.604009 13.43 142.752373
2016-05-04 93.620002 13.31 142.871221
2016-05-05 93.239998 13.32 145.070003
2016-05-06 92.720001 13.44 147.289993"""
df = pd.read_csv(StringIO(text), delim_whitespace=True, parse_dates=[0], index_col=0)
df.div(df.iloc[0]).mul([20, 30, 50])
Upvotes: 3