Featured Article

Create DataFrame Example PySpark: A Complete Guide

Kenneth Jul 13, 2026

In the realm of big data processing, Apache Spark has become a game-changer, and Python, through PySpark, has made it more accessible. One of the core operations you'll perform with PySpark is creating DataFrames, which are central to data processing and analysis. Let's dive into how to create DataFrames using PySpark with practical examples.

PySpark DataFrame Transformations: select vs withColumn
PySpark DataFrame Transformations: select vs withColumn

Before we begin, ensure you have Apache Spark and PySpark installed. You can set up an environment with these tools using tools like Anaconda or directly install them using pip: pip install pyspark

Spark Create DataFrame with Examples
Spark Create DataFrame with Examples

Creating DataFrames from Scratch

One of the most fundamental ways to create a DataFrame in PySpark is by manually defining its schema and data.

Complete Guide on DataFrame Operations in PySpark
Complete Guide on DataFrame Operations in PySpark

Here's an example of creating a DataFrame with columns 'Name', 'Age', and 'Salary':

Defining Schema Manually

PySpark – Create an empty DataFrame
PySpark – Create an empty DataFrame

In this method, you first create a schema, then fill it with data.

from pyspark.sql import SparkSession, types

spark = SparkSession.builder.appName('create_df').getOrCreate()

# Define schema
schema = types.StructType([
    types.StructField('Name', types.StringType()),
    types.StructField('Age', types.IntegerType()),
    types.StructField('Salary', types.FloatType())
])

# Create DataFrame
data = [(1, 'John Doe', 30.0), (2, 'Jane Doe', 35.0)]
df = spark.createDataFrame(data, schema=schema)
df.show()

Using Spark's InferSchema_option

Spark can infer the schema from the data. This is handy when you're not sure about the data types.

Difference between DataFrame, Dataset, and RDD in Spark
Difference between DataFrame, Dataset, and RDD in Spark

# Create DataFrame with inferSchema option
df = spark.createDataFrame(data, inferSchema=True)
df.printSchema()

Loading DataFrames from External Sources

DataFrames aren't always created from scratch; often, they're loaded from external data sources like CSV, JSON, or from a database.

Let's look at an example of loading a CSV file:

Introduction on Apache Spark SQL DataFrame - TechVidvan
Introduction on Apache Spark SQL DataFrame - TechVidvan

Loading a CSV File

PySpark provides the read.csv function to read CSV files.

the data science in sparky poster
the data science in sparky poster
PySpark ArrayType Column With Examples
PySpark ArrayType Column With Examples
How to Pivot and Unpivot a Spark Data Frame - Spark By {Examples}
How to Pivot and Unpivot a Spark Data Frame - Spark By {Examples}
PySpark withColumn() Usage with Examples
PySpark withColumn() Usage with Examples
Evolution of Spark Analytics
Evolution of Spark Analytics
Hadoop vs Spark data processing
Hadoop vs Spark data processing
Client Challenge
Client Challenge
a diagram showing the process of creating an object in which it appears to be written
a diagram showing the process of creating an object in which it appears to be written
GitHub - Kanaries/pygwalker: PyGWalker: Turn your dataframe into an interactive UI for visual analysis
GitHub - Kanaries/pygwalker: PyGWalker: Turn your dataframe into an interactive UI for visual analysis

# Read a CSV file
df = spark.read.csv('path/to/file.csv', header=True, inferSchema=True)
df.show()

Loading JSON Files

Similarly, you can read JSON files using the read.json function.

# Read a JSON file
df = spark.read.json('path/to/file.json')
df.show()

Once you've created or loaded your DataFrame, it's ready for transformations (like select, filter, groupBy) and actions (like show, count, write).

In your data processing journey, you'll create many DataFrames. Remember, the key to efficient data processing lies in creating optimal DataFrames that fit your needs. Happy coding!