Using a multilevel dataframe showing the price and the factor for products alfa - delta, Im trying create a new dataseries with the average price of the two products with highest Factor. Eg, average price of alfa and bravo if alfa and bravo have the highest factors.
import pandas as pd
import numpy as np
index = [np.array(['price', 'price', 'price', 'price', 'factor', 'factor', 'factor', 'factor']),
np.array(['alfa', 'bravo', 'charlie', 'delta', 'alfa', 'bravo', 'charlie', 'delta'])]
df = pd.DataFrame(np.random.randn(3, 8), index=['2014', '2015', '2016'], columns=index)
df
Out[1]:
price factor
alfa bravo charlie delta alfa bravo charlie delta
2014 -1.078024 -2.370577 1.809694 0.937910 0.643634 -1.167022 -0.013712 0.026595
2015 -0.374975 1.459360 0.875787 -1.407601 -1.220319 0.604929 0.414953 0.053431
2016 -0.265826 1.261522 0.839443 -0.144880 0.157955 -1.050584 -0.909444 0.687804