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

重命名Python DataFrame列时出现“positional argument follows keyword argument”语法错误

解决Pandas重命名列时的语法错误问题

错误根源

你写的代码里,columns=后面传了多个独立的字典,但Pandas的rename()方法中,columns参数只接受单个字典作为输入。这种写法会让Python把后面的字典当成「位置参数」,而语法规则里位置参数不能跟在关键字参数(比如columns=这种带=的参数)后面,所以触发了positional argument follows keyword argument错误——和inplace=True无关,就算去掉它,参数传递的语法本身还是错的。

修正后的代码

把所有列名映射合并到同一个字典里,传给columns参数即可:

cardio_df.rename(columns={'ap_hi':'Systolic_BP', 'ap_lo':'Diastolic_BP', 'gluc':'glucose', 'alco':'alcoholic'}, inplace=True)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 08:56:08