【问题标题】:Single positional indexer is out-of-bounds Error单个位置索引器超出范围错误
【发布时间】:2020-12-24 09:13:04
【问题描述】:

我有两个字典,每个字典有 206 个条目。每个团队都是一个数据框。查看图像以了解其中的词典和项目。

我想将所有“电压”字典中的容量值替换如下

x = (total number of rows in the sheet )/25

Capacity Value in Row1(index=0) - the sum of the values from Rows 1 to x in 
                                   "Current" column of 'copy'
Capacity Value in Row2(index=1) - the sum of the values from Rows x to 2*x in 
                                   "Current" column of 'copy'
Capacity Value in Row3(index=2) - the sum of the values from Rows 2*x to 3*x 
                                   in "Current" column of 'copy'
.
.
.
Capacity Value in Row25(index=24) - the sum of the values from Rows 24*x to 
                                     the last value.

为此,我编写了如下代码:

   for sheet in voltage:    
   for sheet in copy:
    n = ( round((len(copy[sheet]))/25) )
    
    for k in range(0,24):
        p = 1+((k+1)*(n))
        
        if k == 24:
            voltage[sheet]['Capacity'].iloc[k] = copy[sheet]['Current'].iloc[((k)*(n)):].sum()
        else:
            voltage[sheet]['Capacity'].iloc[k] = copy[sheet]['Current'].iloc[((k)*(n)+1):(p)].sum()

它显示相同的错误消息。请帮助我。

错误消息:“单个位置索引器超出范围”

谢谢你:)

【问题讨论】:

    标签: python excel dataframe dictionary for-loop


    【解决方案1】:

    发生这种情况是因为列名“当前”下的 Excel 工作表中缺少一些数据。一旦我在 Excel 表中添加了该数据,我就可以正常工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-13
      • 2019-01-11
      • 2018-03-03
      • 2022-07-06
      • 2023-03-30
      相关资源
      最近更新 更多