提问人:Mitchell Walker 提问时间:7/11/2023 更新时间:7/11/2023 访问量:11
转换数据类型 SQL DataFrame
Casting Datatypes SQL DataFrames
问:
我正在尝试在数据帧中读取 databricks 中的 RDD,但无法确定程序为什么将我的整数列读取为字符串。
from pyspark.sql import Row
masterRowsRDD = masterRDD.map(lambda l: Row(playerID=l[0], birthYear=int(l[1]), birthMonth=int(l[2]), birthDay=int(l[3]), birthCountry=l[4], birthState=l[5], birthCity=l[6], deathYear=int(l[7]), deathMonth=int(l[8]), deathDay=int(l[9]), deathCountry=l[10], deathState=l[11], deathCity=l[12], nameFirst=l[13], nameLast=l[14], nameGiven=l[15], weight=int(l[16]), height=int(l[17]), bats=l[18], throws=l[19], debut=date(l[20]), finalGame=date(l[21]), retroID=l[22], bbrefID=l[23]))
我尝试将数据类型直接转换为字段(见上文),并尝试允许 python 解释数据类型(见下文)。
masterRowsRDD = masterRDD.map(lambda l: Row(playerID=l[0], birthYear=l[1], birthMonth=l[2], birthDay=l[3], birthCountry=l[4], birthState=l[5], birthCity=l[6], deathYear=l[7], deathMonth=l[8], deathDay=l[9], deathCountry=l[10], deathState=l[11], deathCity=l[12], nameFirst=l[13], nameLast=l[14], nameGiven=l[15], weight=l[16], height=l[17], bats=l[18], throws=l[19], debut=date(l[20]), finalGame=date(l[21]), retroID=l[22], bbrefID=l[23]))
我在下面创建了一个 Schema 来验证我带入 MasterRowsRDD 的项目。这将验证数据类型是否正确转换。
from pyspark.sql.types import StructType, StructField, LongType, StringType, DateType
masterSchema = StructType( [\
StructField('playerID', StringType(), True), \
StructField('birthYear', LongType(), True), \
StructField('birthMonth', LongType(), True), \
StructField('birthDay', LongType(), True), \
StructField('birthCountry', StringType(), True), \
StructField('birthState', StringType(), True), \
StructField('birthCity', StringType(), True), \
StructField('deathYear', LongType(), True), \
StructField('deathMonth', LongType(), True), \
StructField('deathDay', LongType(), True), \
StructField('deathCountry', StringType(), True), \
StructField('deathState', StringType(), True), \
StructField('deathCity', StringType(), True), \
StructField('nameFirst', StringType(), True), \
StructField('nameLast', StringType(), True), \
StructField('nameGiven', StringType(), True), \
StructField('weight', LongType(), True), \
StructField('height', LongType(), True), \
StructField('bats', StringType(), True), \
StructField('throws', StringType(), True), \
StructField('debut', StringType(), True), \
StructField('finalGame', StringType(), True), \
StructField('retroID', StringType(), True), \
StructField('bbrefID', StringType(), True), \
])
# Making DF
masterDF = spark.createDataFrame(masterRowsRDD, masterSchema)
# print(masterDF.rdd.collect())
masterDF.show()
masterDF.printSchema()
这是我运行程序时显示的错误。任何帮助将不胜感激!
由于阶段故障而中止作业:阶段 48.0 中的任务 0 失败 1 次,最近一次失败:阶段 48.0 (TID 121) 中的任务 0.0 丢失(ip-10-172-222-3.us-west-2.compute.internal 执行程序驱动程序):org.apache.spark.api.python.PythonException:'ValueError:以 10 为基数的 int() 文本无效:'birthMonth'',从 ,第 8 行。完整回溯如下:
答: 暂无答案
评论