读取多个仅在第一个文件中包含标题的CSV文件

2024-04-26 18:04:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有5个CSV文件,标题仅在第一个文件中。我想使用spark读取并创建一个数据帧。我下面的代码可以工作,但是,使用此方法会丢失4行数据,因为在最终读取时,头被设置为true。如果我将头设置为false,我将返回4行数据,但我也将第一个文件的实际头设置为数据中的一行

有没有一种更有效的方法来实现这一点,使标题不会在我的数据集中显示为一行

header = spark.read \
  .format("csv") \
  .option("header", "true") \
  .option("inferSchema", "true") \
  .load("path/file-1") 

schema = header.schema 

df = spark.read \
  .format("csv") \
  .option("header", "true") \
  .schema(schema) \
  .load("path")

Tags: 文件csv数据path方法trueformat标题
1条回答
网友
1楼 · 发布于 2024-04-26 18:04:37

不幸的是,我不认为有一个简单的方法来做你想要的。不过,有一种解决方法看起来与您所做的类似。您可以读取第一个文件以获取模式,读取所有文件,但使用option("header", "false")读取第一个文件,然后将第一个文件与其余文件合并

在python中,它将如下所示:

first_file = "path/file-1"
header = spark.read.option("header", "true") \
  .option("inferSchema", "true").csv(first_file) 
schema = header.schema 

# I use binaryFiles simply to get the list of the files in the folder
# Not that the files are not read.
# Any other mean to list files in a directory would do the trick as well.
all_files = files = spark.sparkContext.binaryFiles("path")\
  .map(lambda x : x[0]).collect()
all_files_but_first = [f for f in all_files if not f.endswith(first_file)]

df = spark.read.option("header", "false") \
  .schema(schema).csv(all_files_but_first)\
  .union(header)

相关问题 更多 >