我将以下数据作为。txt文件,以制表符分隔的格式]存储在我的Blob存储中。我正在使用pyspark.sql将数据作为pyspark.sql.df加载到databricks中。
这是数据的形状。
df = spark.createDataFrame( [ (302, 'foo'), # values (203, 'bar'), (202, 'foo'), (202, 'bar'), (172, 'xxx'), (172, 'yyy'), ], ['LU', 'Input'] # column labels ) display(df)
首先,我在加载之前为数据创建了一个架构:
from pyspark.sql.types import * data_schema = [ StructField('LU', StringType(), True), StructField('Input', StringType(), True)] mySchema = StructType(fields=data_schema)
然后,我使用以下代码读取数据:
df = spark.read.csv("/filepath/filename.txt", schema=mySchema , header=True) df.show()
但是,当我查看数据时,第一列看起来不错,但是第二列值显示为null。
+----------+-----+ | LU|Input| +----------+-----+ |302 | null| |203 | null| |202 | null| |202 | null| |172 | null| |172 | null| +----------+-----+
有人知道为什么'Input'变量显示为null吗?这只是虚拟数据,当使用具有30个以上变量的实数据时,只有第一个变量值加载,其他所有内容均为null。
谢谢
我将以下数据作为.txt文件以Tab分隔格式存储在我的Blob存储中。我正在使用pyspark.sql将数据作为pyspark.sql.df加载到databricks中。这是......的形状>
我计算出数据的问题所在。在我的架构中,我有:
StructField('Date', DateType()
由于文件中已经有标头,所以为什么不让Spark推断模式。我尝试使用您的样本数据,它给出了正确的结果。
>>> df = spark.read.csv("file:////Users/sam/Downloads/file.txt", inferSchema=True, header=True, sep='\t')
>>> df.show()
+---+-----+
| LU|Input|
+---+-----+
|302| foo |
|203| bar|
|202| foo|
|202| bar|
|172| xxx|
|172| yyy|
+---+-----+
>>> df.printSchema()
root
|-- LU: integer (nullable = true)
|-- Input: string (nullable = true)