I have dataframe X
>>> X
A B
x1 x2 intercept x1 x2 intercept
Date
2020-12-31 48.021395 2.406670 1 -11.538462 2.406670 1
2021-03-31 33.229490 2.410444 1 -23.636364 2.405720 1
2021-06-30 11.498812 2.419787 1 -32.727273 2.402403 1
2021-09-30 5.746014 2.583867 1 -34.000000 2.479682 1
2021-12-31 4.612371 2.739457 1 -39.130435 2.496616 1
2022-03-31 3.679404 2.766474 1 -40.476190 2.411736 1
2022-06-30 3.248155 2.771958 1 -45.945946 2.303280 1
and series b:
>>> b
x1 -0.006
x2 0.083
intercept 0.017
I need to compute dot product of each of groups A, B with b, and put the results in one dataframe. I can go through each group explicitly, like the following:
result = pd.concat(
[X["A"].dot(b).rename("A"), X["B"].dot(b).rename("B"),], axis=1,
)
A B
Date
2020-12-31 -0.071375 0.285984
2021-03-31 0.017690 0.358493
2021-06-30 0.148849 0.412763
2021-09-30 0.196985 0.426814
2021-12-31 0.216701 0.459002
2022-03-31 0.224541 0.460031
2022-06-30 0.227584 0.483848
Is there a way to achieve the same without explicitly looping through the groups? In particular, is it possible to first groupby the first level of MultiIndex, then apply the dot product to each group? For example:
result=X.groupby(level=[0], axis=1).apply(lambda x: x.dot(b))
This will give me ValueError: matrices are not aligned
error, which I think is due to the fact that groups in X have two levels of index in its columns whereas b's index is a simple index. So I will need to add a level of index to b to match that in X? Like:
result=X.groupby(level=[0], axis=1).apply(
lambda x: x.dot(pd.concat([b], keys=[x.columns.get_level_values(0)[0]]))
)
With this I get ValueError: cannot reindex from a duplicate axis
. I am getting stuck here.