pandas - 我想将一列的元素复制到另一列,但跳过一个元素并忽略最后一个元素,因为它会产生错误
问题描述
for i in range(len(nifty['Pivot'])):
nifty.p[i] = nifty.Pivot[i+1]
我想跳过最后一个元素并希望将 p 的第一个元素保留为空元素错误:
ValueError: 200 is not in range
The above exception was the direct cause of the following exception:
KeyError: 200
解决方案
那是你的错误Pivot[i+1]
,说如果len(nifty['Pivot'])=200
然后在i=199
, i+1=200
, 并且nifty.Pivot[200]
将失败,因为允许的最大索引是199
(Python 被0
索引)
我想你想要nifty['p'] = nifty['Pivot'].shift(-1)
推荐阅读
- java - spring(无法理解并解决以下错误)
- python - 如何在 Python 中使用 S3 存储桶和 Boto3 将图像上传到 MongoDB
- c# - 需要帮助在 HotChocolate Filters 中创建求和聚合操作
- django - + 不支持的操作数类型:“QuerySet”和“QuerySet”
- excel - 使用 URL 将行数据复制到另一行?
- c# - 在 .Net Core 中捕获 Polly 中的最后一个异常?
- matlab - 如何使用 Doolittle 方法解释此 MATLAB 代码以进行 LU 分解?
- java - 无法将 C++ 项目与 Gradle 正确链接(使用外部系统时出现异常)
- excel - 超过 21 个值的验证下拉列表崩溃
- python - 列表元素未更新?