1

How can I multiply an array to the columns of a dataframe and then sum these columns to a new column in a dataframe?

I tried it with the code below but somehow get wrong numbers:

                  AAPL  Portfolio         ACN
Date                                           
2017-01-03  116.150002  1860.880008  116.459999
2017-01-04  116.019997  1862.079960  116.739998
2017-01-05  116.610001  1852.799992  114.989998
2017-01-06  117.910004  1873.680056  116.300003
...

How it should look like is the following:

                  AAPL  Portfolio         ACN
Date                                          
2017-01-03  116.150002  1046.900003 116.459999
2017-01-04  116.019997  1047.779978 116.739998
2017-01-05  116.610001  1041.389994 114.989998
2017-01-06  117.910004  1053.140031 116.300003
...

The code looks like the following. Might be that I am thinking too complicated and therefore the code makes no sense:

import pandas_datareader.data as pdr
import pandas as pd
import datetime

start = datetime.datetime(2017, 1, 1)
end = datetime.datetime(2017, 3, 17)

ticker_list = ["AAPL","ACN"]
position_size = [4,5]

for i in range(0,len(ticker_list)):
    #print(i)
    DataInitial = pdr.DataReader(ticker_list[i], 'yahoo', start, end)
    ClosingPrices[ticker_list[i]] = DataInitial[['Close']]
    ClosingPrices['Portfolio'] = ClosingPrices['Portfolio'] + ClosingPrices[ticker_list[i]]*position_size[i] 
print(ClosingPrices)

What I want is actually:

2017-01-03: 116.150002*4+116.150002*5
2017-01-03: 116.019997*4+116.739998*5

etc...

4
  • What is your question? Commented Mar 19, 2017 at 18:06
  • @DYZ, as stated above I want to multiply the position to the dedicated prices and then add the column per day together. Is it clearer? Commented Mar 19, 2017 at 18:08
  • This is still not a question. SO is a Q&A site. Unless you ask a clear question, you won't get an answer. Commented Mar 19, 2017 at 18:09
  • @DYZ, I change my description above hope it is now clearer? Commented Mar 19, 2017 at 18:13

1 Answer 1

1

If need:

2017-01-03: 116.150002*4+116.150002*5
2017-01-03: 116.019997*4+116.739998*5

then use concat of multiple columns by value from dict and last sum all columns together:

ticker_list = ["AAPL","ACN"]
position_size = [4,5]
d = dict(zip(ticker_list,position_size))

print (pd.concat([ClosingPrices[col] * d[col] for col in ticker_list], axis=1))
                  AAPL         ACN
Date                              
2017-01-03  400.000000  500.000000
2017-01-04  464.079988  583.699990
2017-01-05  466.440004  574.949990
2017-01-06  471.640016  581.500015

ClosingPrices['Portfolio'] = pd.concat([ClosingPrices[col] * d[col] for col in ticker_list], 
                                        axis=1).sum(axis=1)
print (ClosingPrices)
                  AAPL    Portfolio         ACN
Date                                           
2017-01-03  100.000000   900.000000  100.000000 <-for testing values was changed to 100
2017-01-04  116.019997  1047.779978  116.739998
2017-01-05  116.610001  1041.389994  114.989998
2017-01-06  117.910004  1053.140031  116.300003
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot this is what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.