gpt4 book ai didi

python - Pandas 分组7天

转载 作者:行者123 更新时间:2023-12-02 04:46:29 26 4
gpt4 key购买 nike

如何按周汇总我的数据计数,如果上周仍未完成,则计算平均“标准化”假设这些是我的列表

days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
counts = [1839,1334,2241,2063,1216,1409,1614,1860,1298,1140,1122,2153,971,1650,1835,889,653,484,2078,1198,426,684,910,701,851,360,763,402,1853,400,1159]

谢谢

最佳答案

这是 Pandas 的解决方案:

1)创建数据框:

df = pd.DataFrame({'days':days,'counts': counts})
df['week'] = df.days.sub(1)//7 # adding week column

2)按周计算总和和平均值,然后生成归一化总和:

d2 = df.groupby('week').agg({'counts':['sum','mean']}) # ca
d2['norm_sum'] = d2[('counts','mean')] * 7

3)输出:

print (d2)

counts norm_sum
sum mean
week
0 10102 1683.666667 11785.666667
1 10158 1451.142857 10158.000000
2 8787 1255.285714 8787.000000
3 4695 670.714286 4695.000000
4 3814 953.500000 6674.500000

关于python - Pandas 分组7天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59908180/

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