Now I have a pandas dataframe that is similar to the table
| index | Date | return |
|---|---|---|
| 1 | 2017-1 | 0.1 |
| 1 | 2017-2 | 0.2 |
| 1 | 2017-3 | 0.3 |
| 2 | 2017-1 | 0.4 |
| 2 | 2017-2 | 0.5 |
| 2 | 2017-3 | 0.6 |
| 3 | 2017-1 | 0.7 |
| 3 | 2017-2 | 0.8 |
| 3 | 2017-3 | 0.9 |
| 4 | 2017-1 | 1.0 |
| 4 | 2017-2 | 1.1 |
| 4 | 2017-3 | 1.2 |
And now the table contains 4 indexs in such order. I want constuct portfolio on every 2 index, keeping the date unchanged but portfolio return is the average of 2 index. So that the result will be 2 portfolios.
The target result is
| portfolio_index | Date | portfolio return |
|---|---|---|
| portfolio 1 | 2017-1 | 0.25 |
| portfolio 1 | 2017-2 | 0.35 |
| portfolio 1 | 2017-3 | 0.45 |
| portfolio 2 | 2017-1 | 0.85 |
| portfolio 2 | 2017-2 | 0.95 |
| portfolio 2 | 2017-3 | 1.05 |
For example, the portfolio 1 should contains index 1 and 2, and have return on separate months. Is there any possible solution?
import pandas as pd
db = pd.read_csv('17base.csv')
db = db.groupby(db['index'])['return'].mean()