Setting values in a pandas multi-index cross-sectional slice

Question:

I would like to set the value of a cross section to the value relative to the mean. The code below sets the values to null, but I would like the values to be -5 and 5. Is there an easily readable way to do this without looping through each column in the index?

import pandas as pd

x = pd.DataFrame({'a': [1, 2, 3], 'b': [1, 2, 3]})
y = pd.DataFrame({'a': [11, 12, 13], 'b': [21, 22, 23]})

df = pd.concat({'x': x, 'y': y}, axis=1)

timeslice = df.loc[1, (slice(None), 'a')].values.flatten()
timeslice = timeslice[~np.isnan(timeslice)]
average = np.mean(timeslice)
df.loc[1, (slice(None), 'b')] = df.loc[1, (slice(None), 'a')] - average
   x        y
   a    b   a     b
0  1  1.0  11  21.0
1  2  NaN  12   NaN
2  3  3.0  13  23.0
Asked By: HAL

||

Answers:

The issue is your index alignement. Your two slices don’t align resulting in a NaN.

Use:

df.loc[1, (slice(None), 'b')] = df.loc[1, (slice(None), 'a')].to_numpy() - average

Output:

   x      y    
   a  b   a   b
0  1  1  11  21
1  2 -5  12   5
2  3  3  13  23
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.