【问题标题】:Finding Running Standard Deviation for Values in Pandas DataFrame with Binned Values使用分箱值查找 Pandas DataFrame 中值的运行标准偏差
【发布时间】:2020-08-12 16:09:38
【问题描述】:

我有一个 DataFrame limitdf,它的值在 31 个 bin 中分箱,在列 MASS_BINS 中标记。我想按 bin 查找对象的此 DataFrame 中其他列的均值和标准差。我尝试为每个 bin 生成一组元组 (mean , std)

stdarray = []
for i in range(0 , 31):
    stdarray[i] = [(limitdf['td_lmass'][limitdf.MASS_BINS == i].mean(axis = 0) , limitdf['PAB_SFR_EX2'][limitdf.MASS_BINS == i].std(axis = 0))]

这用IndexError: list assignment index out of range 对我大喊大叫。为什么这不起作用,或者有更好更简单的方法吗?

【问题讨论】:

    标签: python pandas statistics astronomy


    【解决方案1】:

    你试过熊猫的DataFrame.rolling吗?看看this

    【讨论】:

      【解决方案2】:

      我相信这不是 pandas 的问题,而是实际上与您尝试在 stdarray 中为索引 i 赋值有关,而 stdarray 是长度为 0 的列表。

      用简单的语言如果i=0,你对python说的是“请用x替换stdarray的元素0”。但是stdarray 没有元素 0,所以 python 崩溃了。

      因为您正试图将 assign 某些东西发送到 list indexout of range

      要解决您的问题,您应该尝试stdarray.append(YOUR_CODE_HERE) 而不是stdarray[i] = YOUR_CODE_HERE

      【讨论】:

        猜你喜欢
        • 2018-08-06
        • 2016-11-25
        • 1970-01-01
        • 1970-01-01
        • 2021-06-14
        • 1970-01-01
        • 2012-12-03
        • 2012-08-16
        • 2023-03-27
        相关资源
        最近更新 更多