gpt4 book ai didi

python - 我如何计算每组的移动扩展平均值

转载 作者:太空宇宙 更新时间:2023-11-04 01:50:35 25 4
gpt4 key购买 nike

我想基于 groupby('col1') 扩展 col2 的均值,但我希望均值不包括行本身(仅包括其上方的行)

dummy = pd.DataFrame({"col1": ["a",'a','a','b','b','b','c','c'],"col2":['1','2','3','4','5','6','7','8'] }, index=list(range(8)))
print(dummy)
dummy['one_liner'] = dummy.groupby('col1').col2.shift().expanding().mean().reset_index(level=0, drop=True)
dummy['two_liner'] = dummy.groupby('col1').col2.shift()
dummy['two_liner'] = dummy.groupby('col1').two_liner.expanding().mean().reset_index(level=0, drop=True)
print(dummy)
---------------------------
here is result of first print statement:
col1 col2
0 a 1
1 a 2
2 a 3
3 b 4
4 b 5
5 b 6
6 c 7
7 c 8
here is result of the second print:
col1 col2 one_liner two_liner
0 a 1 NaN NaN
1 a 2 1.000000 1.0
2 a 3 1.500000 1.5
3 b 4 1.500000 NaN
4 b 5 2.333333 4.0
5 b 6 3.000000 4.5
6 c 7 3.000000 NaN
7 c 8 3.800000 7.0

我原以为他们的结果是一样的。two_liner 是预期的结果。 one_liner 在组之间混合数字。

想出这个解决方案花了很长时间,谁能解释一下逻辑?为什么 one_liner 没有给出预期的结果?

最佳答案

您正在 groupby() 中寻找 expanding().mean()shift():

groups = df.groupby('col1')
df['one_liner'] = groups.col2.apply(lambda x: x.expanding().mean().shift())

df['two_liner'] = groups.one_liner.apply(lambda x: x.expanding().mean().shift())

输出:

  col1  col2  one_liner  two_liner
0 a 1 NaN NaN
1 a 2 1.0 NaN
2 a 3 1.5 1.0
3 b 4 NaN NaN
4 b 5 4.0 NaN
5 b 6 4.5 4.0
6 c 7 NaN NaN
7 c 8 7.0 NaN

解释:

(dummy.groupby('col1').col2.shift()   # this shifts col2 within the groups 
.expanding().mean() # this ignores the grouping and expanding on the whole series
.reset_index(level=0, drop=True) # this is not really important
)

所以上面的链式命令等同于

s1 = dummy.groupby('col1').col2.shift()
s2 = s1.expanding.mean()
s3 = s2.reset_index(level=0, drop=True)

如您所见,只有 s1 考虑按 col1 分组。

关于python - 我如何计算每组的移动扩展平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58127106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com