Load CSV File in PySpark

access_time 6 months ago visibility2988 comment 0

CSV is a commonly used data format. Spark provides rich APIs to load files from HDFS as data frame.  This page provides examples about how to load CSV from HDFS using Spark. If you want to read a local CSV file in Python, refer to this page Python: Load / Read Multiline CSV File instead.

Sample data file

The CSV file content looks like the following:

1,Record 1,Hello World!
2,Record 2,Hello Hadoop!
3,Record 3,"Hello 
4,Record 4,Hello!
For the third record, field Text2 is across two lines. 
The file is ingested into my Hadoop instance with location as:
hadoop fs -copyFromLocal data.csv /data.csv

Load CSV file

We can use 'read' API of SparkSession object to read CSV with the following options:

  • header = True: this means there is a header line in the data file.
  • sep=, : comma is the delimiter/separator. Since our file is using comma, we don't need to specify this as by default is is comma.
  • multiLine = True: this setting allows us to read multi-line records. 

Sample code

from pyspark.sql import SparkSession

appName = "Python Example - PySpark Read CSV"
master = 'local'

# Create Spark session
spark = SparkSession.builder \
    .master(master) \
    .appName(appName) \

# Convert list to data frame
df = spark.read.format('csv') \
                .option('header',True) \
                .option('multiLine', True) \
print(f'Record count is: {df.count()}')


The output looks like the following:

| ID|   Text1|          Text2|
|  1|Record 1|   Hello World!|
|  2|Record 2|  Hello Hadoop!|
|  3|Record 3|Hello
|  4|Record 4|         Hello!|
Record count is: 4
info Last modified by Raymond 5 months ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

More from Kontext

visibility 26606
thumb_up 4
access_time 2 years ago

Spark is an analytics engine for big data processing. There are various ways to connect to a database in Spark. This page summarizes some of common approaches to connect to SQL Server using Python as programming language. For each method, both Windows Authentication and SQL Server ...

visibility 3655
thumb_up 0
access_time 2 years ago

This page summarizes the steps to install Zeppelin version 0.7.3 on Windows 10 via Windows Subsystem for Linux (WSL). When running Zeppelin in Ubuntu, the server may pick up one host address that is not accessible, for example, and the the remote interpreter connection cannot be ...

visibility 10628
thumb_up 0
access_time 2 years ago

For SQL developers that are familiar with SCD and merge statements, you may wonder how to implement the same in big data platforms, considering database or storages in Hadoop are not designed/optimised for record level updates and inserts. In this post, I’m going to demonstrate how to implement ...