在 Pandas 多级索引的横截面切片中设置数值。

huangapple go评论68阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月27日 03:52:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定