You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

删除Pandas DataFrame行操作失效,请求技术排查

问题分析与解决

问题原因

你的DataFrame索引是整数类型(通过np.arange(101,106,1)生成的是整数数组),但调用drop方法时传入的是字符串'103',索引类型不匹配,导致Pandas无法定位到目标行,进而触发报错。

解决方法

有两种可行的修复方式:

方法1:传入匹配类型的索引值

直接使用整数类型的索引103替代字符串'103':

import numpy as np
import pandas as pd
randArr = np.random.randint(0,100,20).reshape(5,4)
df = pd.DataFrame(randArr, np.arange(101,106,1), ['PDS', 'Algo','SE','INS'])
df.drop(103, inplace=True)

方法2:提前将索引转为字符串类型

如果业务场景需要使用字符串索引,创建DataFrame时将索引数组转为字符串类型:

import numpy as np
import pandas as pd
randArr = np.random.randint(0,100,20).reshape(5,4)
# 将索引转为字符串
df = pd.DataFrame(randArr, np.arange(101,106,1).astype(str), ['PDS', 'Algo','SE','INS'])
df.drop('103', inplace=True)

验证方式

可以通过以下代码查看索引的类型,确认是否匹配:

print(type(df.index[0]))

原代码中会输出<class 'int'>,说明索引是整数类型。

内容的提问来源于stack exchange,提问作者Subrata Biswas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 12:39:51