英文:
Setting values in a pandas multi-index cross-sectional slice
问题
我想将横截面的值设置为相对于均值的值。下面的代码将值设置为null,但我想将值设置为-5和5。是否有一种易于阅读的方法来做到这一点,而不需要遍历索引中的每一列?
```import numpy as np
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
英文:
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
答案1
得分: 1
问题出在你的索引对齐上。你的两个切片没有对齐,导致出现NaN。
使用以下代码:
df.loc[1, (slice(None), 'b')] = df.loc[1, (slice(None), 'a')].to_numpy() - average
输出:
x y
a b a b
0 1 1 11 21
1 2 -5 12 5
2 3 3 13 23
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论