首页 > 解决方案 > Pyspark - 为数据框定义自定义模式

问题描述

我正在尝试读取 csv 文件,并尝试将其存储在数据框中,但是当我尝试创建IDtype 的列时StringType,它并没有以预期的方式发生。

table_schema = StructType([StructField('ID', StringType(), True),
                     StructField('Name', StringType(), True),
                     StructField('Tax_Percentage(%)', IntegerType(), False),
                     StructField('Effective_From', TimestampType(), False),
                     StructField('Effective_Upto', TimestampType(), True)])

# CSV options
infer_schema = "true"
first_row_is_header = "true"
delimiter = ","


df = spark.read.format(file_type) \
  .option("inferSchema", infer_schema) \
  .option("header", first_row_is_header) \
  .option("sep", delimiter) \
  .option("schema", table_schema) \
  .load(file_location)



display(df)

以下是运行上述代码后生成的架构:

df:pyspark.sql.dataframe.DataFrame
ID:integer
Name:string
Tax_Percentage(%):integer
Effective_From:string
Effective_Upto :string

尽管提供了自定义架构,但仍将其ID键入到integer我期望它为字符串的位置。与列Effective_From和相同Effective_Upto

标签: pythonpyspark

解决方案


它应该是

.schema(table_schema) \

代替

.option("schema", table_schema) \

.option("inferSchema", "true") \此外,如果您提供架构定义,则不需要:)


推荐阅读