在Pyspark中以正确的数据类型读取CSV

时间:2018-10-26 16:48:00

标签: csv pyspark pyspark-sql

当我尝试导入带有spark的本地CSV时,默认情况下每一列都以字符串形式读取。但是,我的列仅包含整数和时间戳类型。更具体地说,CSV如下所示:

"Customer","TransDate","Quantity","PurchAmount","Cost","TransID","TransKey"
149332,"15.11.2005",1,199.95,107,127998739,100000

我发现应该在this question中工作的代码,但是当我执行它时,所有条目都以NULL的形式返回。

我使用以下内容创建自定义架构:

from pyspark.sql.types import LongType, StringType, StructField, StructType, BooleanType, ArrayType, IntegerType, TimestampType

customSchema = StructType(Array(
        StructField("Customer", IntegerType, true),
        StructField("TransDate", TimestampType, true),
        StructField("Quantity", IntegerType, true),
        StructField("Cost", IntegerType, true),
        StructField("TransKey", IntegerType, true)))

,然后使用以下命令读取CSV文件:

myData = spark.read.load('myData.csv', format="csv", header="true", sep=',', schema=customSchema)

哪个返回:

+--------+---------+--------+----+--------+
|Customer|TransDate|Quantity|Cost|Transkey|
+--------+---------+--------+----+--------+
|    null|     null|    null|null|    null|
+--------+---------+--------+----+--------+

我错过了关键的一步吗?我怀疑日期列是问题的根源。注意:我正在GoogleCollab中运行它。

3 个答案:

答案 0 :(得分:2)

您在这里!

"Customer","TransDate","Quantity","PurchAmount","Cost","TransID","TransKey"
149332,"15.11.2005",1,199.95,107,127998739,100000
PATH_TO_FILE="file:///u/vikrant/LocalTestDateFile"
Loading above file to dataframe:
df = spark.read.format("com.databricks.spark.csv") \
  .option("mode", "DROPMALFORMED") \
  .option("header", "true") \
  .option("inferschema", "true") \
  .option("delimiter", ",").load(PATH_TO_FILE)

您的日期将作为字符串列类型加载,但是当您将其更改为日期类型时,它将将该日期格式视为NULL。

df = (df.withColumn('TransDate',col('TransDate').cast('date'))

+--------+---------+--------+-----------+----+---------+--------+
|Customer|TransDate|Quantity|PurchAmount|Cost|  TransID|TransKey|
+--------+---------+--------+-----------+----+---------+--------+
|  149332|     null|       1|     199.95| 107|127998739|  100000|
+--------+---------+--------+-----------+----+---------+--------+

因此,我们需要将日期格式从dd.mm.yy更改为yy-mm-dd。

from datetime import datetime
from pyspark.sql.functions import col, udf
from pyspark.sql.types import DateType
from pyspark.sql.functions import col

更改日期格式的Python函数:

  change_dateformat_func =  udf (lambda x: datetime.strptime(x, '%d.%m.%Y').strftime('%Y-%m-%d'))

立即为您的数据框列调用此函数:

newdf = df.withColumn('TransDate', change_dateformat_func(col('TransDate')).cast(DateType()))

+--------+----------+--------+-----------+----+---------+--------+
|Customer| TransDate|Quantity|PurchAmount|Cost|  TransID|TransKey|
+--------+----------+--------+-----------+----+---------+--------+
|  149332|2005-11-15|       1|     199.95| 107|127998739|  100000|
+--------+----------+--------+-----------+----+---------+--------+

以及以下是架构:

 |-- Customer: integer (nullable = true)
 |-- TransDate: date (nullable = true)
 |-- Quantity: integer (nullable = true)
 |-- PurchAmount: double (nullable = true)
 |-- Cost: integer (nullable = true)
 |-- TransID: integer (nullable = true)
 |-- TransKey: integer (nullable = true)

让我知道它是否对您有用。

答案 1 :(得分:0)

尝试使用RDD,然后使用格式正确的lambda将其重新格式化为YYYY-MM-DD,然后将其转换为数据框。让我看看是否可行,或者您需要为此编写代码。

答案 2 :(得分:0)

您可以为('dateFormat','d.M.y')指定一个DataFrameReader选项,以特定格式解析日期。

df = spark.read.format("csv").option("header","true").option("dateFormat","M.d.y").schema(my_schema).load("path_to_csv")

参考

https://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.DataFrameReader https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html