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

报错TypeError: 'method' object is not subscriptable,求Pandas导出CSV代码修复

解决TypeError: 'method' object is not subscriptable报错问题

嘿,这个问题我熟!你遇到的TypeError: 'method' object is not subscriptable报错,根源是把pandas里的方法调用语法搞错啦😉

错误原因

在pandas中,set_index和to_csv都是DataFrame对象的方法,调用方法必须使用圆括号()来传递参数;而你用了方括号[](方括号是用来做索引取值的),这就导致Python把它们当成了可索引的对象,而实际上方法本身不能被方括号索引,所以抛出了这个错误。

修正后的代码

pd.DataFrame({'Pokemon_name': test.Pokemon_name, 'CP': new_pred_class}).set_index('Pokemon_name').to_csv('new_pred_cp.csv')

具体修改点

  • 将set_index['Pokemon_name']改为set_index('Pokemon_name'):用圆括号传入要设置为索引的列名参数
  • 将to_csv['new_pred_cp.csv']改为to_csv('new_pred_cp.csv'):用圆括号传入要保存的CSV文件名参数

这样修改后,代码就能正常执行,把包含宝可梦名称和预测CP值的数据写入到指定的CSV文件里啦。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:28:23