Perform a calculation as a new column on multiindex dataframe

I have the following multiindex dataframe: MSISDN_COUNT NAME DIM1 SEGMENT JAN22 N Detractor 307 Passive 198 Promoter 1256 Y Detractor 237 Passive 161 Promoter 1096 FEB22 N Detractor 313 Passive 222 Promoter 1260 Y Detractor 261 Passive 169 Promoter 1155 I want to create a new column, which will be calculated for each NAME and… Read More Perform a calculation as a new column on multiindex dataframe

Pivoting first level of a multilevel index to be the first level of a multilevel column

I have a multilevel index dataframe like this: indx = [(‘location’, ‘a’), (‘location’, ‘b’), (‘location’, ‘c’), (‘location2’, ‘a’), (‘location2’, ‘b’), (‘location2’, ‘c’)] indx = pd.MultiIndex.from_tuples(indx) col = [‘S1′,’S2′,’S3’] df = pd.DataFrame(np.random.randn(6, 3), index=indx, columns=col) df S1 S2 S3 location a -0.453549 -0.079797 0.581657 b -0.458573 -0.732625 -2.277674 c 0.874403 0.459590 -1.220271 location2 a -1.418821 0.847556… Read More Pivoting first level of a multilevel index to be the first level of a multilevel column