2 df_array[i][5]-2013==int(df_array[i][5]-2013) 3 for i in range(len(df_array)): ----> 4 new_array[df_array[i][5]-2013][df_array[i][6]-1][df_array[i][1]][df_array[i][2]]+=df_array[i][3] 5 df_new = pd.DataFrame(columns=['创建年', '创建月', '物料编号', '应发库', '销售订单数量']) 6 i=0 IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
时间: 2023-10-08 08:11:45 浏览: 55
根据你提供的代码和错误信息,很可能是在第4行代码中出现了错误。将列表或数组的非整数值用作索引会导致“IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices”错误。请检查df_array[i][5]和df_array[i][6]是否是整数值,如果不是,请将其转换为整数值。
相关问题
df_array[i][5]= int(df_array[i][5]) 6 df_array[i][6] = int(df_array[i][6]) ----> 7 new_array[df_array[i][5]-2013][df_array[i][6]-1][df_array[i][1]][df_array[i][2]]+=df_array[i][3] 8 df_new = pd.DataFrame(columns=['创建年', '创建月', '物料编号', '应发库', '销售订单数量']) 9 i=0 IndexError: index -1953 is out of bounds for axis 0 with size 4
根据报错信息,问题可能出现在第7行的代码中,因为它尝试访问一个超出数组索引范围的元素。具体来说,它尝试访问一个小于0或大于数组大小的索引值。你需要检查数组的大小和索引值,并确保索引值不会超出数组范围。此外,根据第8行的代码,你需要创建一个名为`df_new`的空数据框架来存储数据。如果数据框架未创建,则可能会导致`IndexError`。
new_array=np.zeros((4,12,100,9)) 2 for i in range(len(df_array)): ----> 3 new_array[df_array[i][5]-2013][df_array[i][6]-1][df_array[i][1]][df_array[i][2]]+=df_array[i][3] 4 df_new = pd.DataFrame(columns=['创建年', '创建月', '物料编号', '应发库', '销售订单数量']) IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices需要如何更改
这个错误是因为在使用索引时,使用了非整数类型的值。可能是因为df_array中的某些值不是整数类型导致的。你可以使用astype()方法将其转换为整数类型。另外,代码中的第3行需要将df_array的长度存储在变量中,以避免在循环中多次计算。下面是修改后的代码:
```
df_array = df_array.astype(int)
array_len = len(df_array)
new_array = np.zeros((4, 12, 100, 9))
for i in range(array_len):
new_array[df_array[i][5]-2013][df_array[i][6]-1][df_array[i][1]][df_array[i][2]] += df_array[i][3]
df_new = pd.DataFrame(columns=['创建年', '创建月', '物料编号', '应发库', '销售订单数量'])
```
希望能对你有所帮助。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)