Python numpy '数组索引太多'

我想用数据框调用中的系列填充二维数组。当我执行以下代码时,出现错误“数组索引过多”。当我手动将形状设置为系列大小时,它突然切换到一个更大的位置。(该系列大约有 356 个位置)


size_arr = np.empty(shape=(len(business_date_list)))

y_arr = np.empty(shape=(len(business_date_list)))


for i in range(0, len(business_date_list)):

    news = model_data['size'].loc[(model_data['date'] == business_date_list[i])]

    size_arr[i,:] = news

    newy = model_data['changeday'].loc[(model_data['date'] == business_date_list[i])]

    y_arr[i,:] = newy


慕慕森
浏览 102回答 2
2回答

MYYA

这会重现您的错误消息。您应该显示完整的消息,包括回溯。它包含对您和我们都很有价值的信息!In [332]: np.empty(3)[0,:]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;---------------------------------------------------------------------------IndexError&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Traceback (most recent call last)<ipython-input-332-c37e54b88567> in <module>----> 1 np.empty(3)[0,:]IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed你定义一个一维数组size_arr = np.empty(shape=(len(business_date_list)))并尝试将其索引为 2dsize_arr[i,:] = news

哈士奇WWW

索引不是问题,而是正确调整大小shape = ((len(business_date_list)),500)size_arr = np.empty(shape=(shape))y_arr = np.empty(shape=(shape))news = np.empty(shape=(500))#newy = np.empty(shape=(500,))for b in range(0, len(business_date_list)):&nbsp; &nbsp; news = model_data['size'].loc[(model_data['date'] == business_date_list[b])]&nbsp; &nbsp; c = 500-len(news)&nbsp; &nbsp; news = np.pad(news, (0,c), 'empty')&nbsp; &nbsp; size_arr[b,:] = news&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; newy = model_data['changeday'].loc[(model_data['date'] == business_date_list[b])]&nbsp; &nbsp; d = 500-len(newy)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; newy = np.pad(newy, (0,d), 'empty')&nbsp; &nbsp; y_arr[b,:] = newy
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python