(+351) 21 24 10006  ·  info@bconcepts.pt
Carnaxide, Lisbon

How to read a CSV file into a DataFrame in Databricks

João Barros 07 de July de 2026 4 min read

Reading a CSV file into a DataFrame is the first step in almost every data job in Databricks. Once the data is in a DataFrame, you can filter it, transform it, and save it to Delta tables. The CSV format is still one of the most common ways to exchange data, so importing it correctly avoids many problems later on. This guide shows, in a simple and repeatable way, how to upload a CSV and read it with PySpark, even if you are taking your first steps on the platform.

Prerequisites

  • Access to a Databricks workspace with permission to run a notebook.
  • A cluster attached to the notebook (in the running state).
  • A CSV file to upload, for example vendas.csv, with the column names on the first line.

Step 1: Upload the file to Databricks

Before you can read the file, it must be accessible to the cluster. The simplest way is through the interface: in the sidebar, open Catalog and upload the CSV to a Unity Catalog volume. The file becomes available at a path like /Volumes/main/default/dados/vendas.csv. In older workspaces you can use DBFS, with a path like /FileStore/tables/vendas.csv. You can drag the file into the upload window or pick it from your computer; keep the path shown, because you will need it in the reading step.

Step 2: Create a notebook

Create a new notebook and attach it to the cluster. Set the default language to Python. You will use the spark variable, which already exists in every Databricks notebook and represents the active Spark session — you do not need to create it.

Step 3: Read the CSV into a DataFrame

Use spark.read with two important options: header, to treat the first line as column names, and inferSchema, so Spark automatically deduces the data types (numbers, dates, and text):

df = (spark.read
      .format("csv")
      .option("header", "true")
      .option("inferSchema", "true")
      .load("/Volumes/main/default/dados/vendas.csv"))

There is also a shorter form, with exactly the same result. Storing the result in a variable named df is just a convention; you can use any name:

df = spark.read.csv(
    "/Volumes/main/default/dados/vendas.csv",
    header=True, inferSchema=True)
Tip: the inferSchema option makes Spark read the file twice. For very large files, defining the schema by hand is faster.

Step 4: View the data and confirm the types

To inspect the content in an interactive table, use display. To see the column structure and the types Spark deduced, use printSchema:

display(df)
df.printSchema()

If a numeric value was read as text, the cause is usually the separator. Many files use a semicolon instead of a comma; in that case, add .option("sep", ";") to the read.

Step 5: Save the DataFrame to a Delta table (optional)

If you want to reuse the data in other notebooks or query it in SQL, save the DataFrame as a managed table in Delta format. The data is then versioned and available to the whole team:

df.write.format("delta").saveAsTable("vendas")

From that point on, you can query the table in a SQL cell with a SELECT statement over the vendas table.

Check the result

Confirm that the data was read as you expected. The df.count() function returns the number of rows, and df.show(5) displays the first five:

print(df.count())
df.show(5)

Compare the number of rows with the size of the original file; if they differ a lot, there may be malformed rows or the wrong separator. If the row total is zero, or if the column names appear as _c0 and _c1, review the file path and confirm that the header option is enabled.

Conclusion

You now have a CSV file turned into a DataFrame ready to use and, if you chose to, saved as a Delta table you can query in SQL. From here you can handle missing values, convert types, or join other tables for your analysis. What will be the first question you ask this data?