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

修改Spark DataFrame列类型时遇AttributeError:无withcolumn属性

解决Spark DataFrame的AttributeError: 'DataFrame' object has no attribute 'withcolumn'问题

错误原因很明确:你写错了PySpark DataFrame的方法名。正确的方法是withColumn(第二个字母C大写),而非你代码里的withcolumn(全小写)。

另外还要注意,你的代码里没有导入IntegerType,如果之前没在其他地方导入,运行时还会触发新的错误,需要补充导入语句。

修正后的完整代码:

from pyspark.sql.functions import col
from pyspark.sql.types import IntegerType

newDF = newDF.withColumn("Maxpulse", col("Maxpulse").cast(IntegerType()))

PySpark的API方法名普遍遵循驼峰命名规则,比如groupBy、orderBy都是如此,拼写时要严格区分大小写,否则就会出现找不到属性的报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 03:52:03