修改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
相关产品推荐
相关产品推荐

