cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Load csv file as a dataframe?

Kaniz_Fatma
Community Manager
Community Manager
 
3 REPLIES 3

Kaniz_Fatma
Community Manager
Community Manager

spark-csv is part of core Spark functionality and doesn't require a separate library.

df = spark.read.format("csv").option("header", "true").load("file.csv")

Kaniz_Fatma
Community Manager
Community Manager

In scala,(this works for any format-in delimiter mention "," for csv, "\t" for tsv etc)

val df = sqlContext.read.format("com.databricks.spark.csv")

.option("delimiter", ",")

.load("csvfile.csv")

Hubert-Dudek
Esteemed Contributor III

as @Kaniz Fatmaโ€‹ wrote you can use native functions for it:

df = spark.read.format("csv").option("header", "true").load("file.csv")

Alternative really nice way is to use sql syntax for that:

%sql
CREATE TEMPORARY VIEW diamonds
USING CSV
OPTIONS (path "/databricks-datasets/Rdatasets/data-001/csv/ggplot2/diamonds.csv", header "true", mode "FAILFAST")

Here is spark documentation:

https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.sql.DataFrameReader.csv.html?h...

and databricks documentation:

https://docs.databricks.com/data/data-sources/read-csv.html

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group