I don't know where to start but I have data for two stock portfolios that I need to combine to represent one portfolio. Below is the dataframe that I'm starting with and also that I want to end up with.
Here's the data I already have
rawdata = {'portfolio': ['port1', 'port2', 'port1', 'port2'],
'portfolioname': ['portfolioone', 'portfoliotwo', 'portfolioone', 'portfoliotwo'],
'date': ['04/12/2020', '04/12/2020', '04/12/2020', '04/12/2020'],
'code': ['ABC', 'ABC', 'XYZ', 'XYZ'],
'quantity': [2, 3, 10, 11],
'price': [1.5, 1.5, 0.2, 0.2],
'value': [3, 4.5, 2, 2.2],
'weight': [.6, .67, .4, .328]}
df1 = pd.DataFrame(rawdata)
Here's the data that I want to create
finisheddata = {'portfolio': ['port3', 'port3'],
'portfolioname': ['portfoliothree', 'portfoliothree'],
'date': ['04/12/2020', '04/12/2020'],
'code': ['ABC', 'XYZ'],
'quantity': [5, 21],
'price': [1.5, 0.2],
'value': [7.5, 4.2],
'weight': [.64, .36]}
df2 = pd.DataFrame(finisheddata)
So what I'm trying to do is to group the two portfolios together by 'code' where the 'portfolio' and 'portfolioname' are arbitary, 'date' is always the same for both portfolios, 'quantity' is a sum, 'price' is taken from either port1 or port2, 'value' is 'price' x 'quantity' and 'weight' is 'value' divided by the sum of the portfolio.
Thanks very very much.
port5 + port6 = port11andportfolioone + portfoliofive = portfoliosix. Currently works only for single digits and their sum, so beware.