โ† Back to list

Working with Spark DataFrames: A Better Way to Handle Data ๐Ÿš€๐Ÿ“Š

Introduction

Krishnatej Sreeramula ยท 2025-01-02 07:23 ยท 2 claps ยท 2.2 min read paywalled
#data #dataframes #spark-dataframe #pyspark #data-engineering
Open on Medium โ†—
Wiki topics: ๐Ÿ”ง ยท Data Engineering

Working with Spark DataFrames: A Better Way to Handle Data ๐Ÿš€๐Ÿ“Š

Introduction

Apache Spark is a robust tool for handling big data, and DataFrames have become one of its most popular abstractions for data manipulation. In this blog, weโ€™ll explore how DataFrames make working with data easier and more efficient, their differences from RDDs and Datasets, and practical ways to create and manipulate them.

1๏ธโƒฃ What Are Spark DataFrames?

A DataFrame is a distributed collection of data organized into named columns, similar to a table in a relational database or a dataframe in Pythonโ€™s pandas. Key Features:

  • Schema-based: Columns have names and data types.
  • Optimized for SQL-style queries and transformations.
  • Built on top of Sparkโ€™s RDDs (Resilient Distributed Datasets).

2๏ธโƒฃ RDDs vs. DataFrames vs. Datasets ๐Ÿ› ๏ธ

Why prefer DataFrames?

  • Optimized query execution.
  • Supports SQL-like queries with spark.sql.
  • Easy to manipulate using transformations and actions.

3๏ธโƒฃ Creating Spark DataFrames

From a File (CSV, JSON, Parquet)

from pyspark.sql import SparkSession  
spark = SparkSession.builder.appName("DataFrameExample").getOrCreate()  
# Reading a CSV file  
df = spark.read.csv("data.csv", header=True, inferSchema=True)  
df.show()

From an RDD

rdd = spark.sparkContext.parallelize([(1, "Alice"), (2, "Bob")])  
columns = ["ID", "Name"]  
df = spark.createDataFrame(rdd, schema=columns)  
df.show()

From a Python Dictionary

data = [{"Name": "Alice", "Age": 30}, {"Name": "Bob", "Age": 25}]  
df = spark.createDataFrame(data)  
df.show()

4๏ธโƒฃ Manipulating DataFrames

Basic Transformations

# Selecting specific columns  
df.select("Name").show()  

# Filtering rows  
df.filter(df["Age"] > 25).show()  

# Adding a new column  
df = df.withColumn("Senior", df["Age"] > 28)  
df.show()  

# Dropping a column  
df = df.drop("Senior")  
df.show()

SQL Queries

df.createOrReplaceTempView("people")  
result = spark.sql("SELECT Name, Age FROM people WHERE Age > 25")  
result.show()

Aggregations

from pyspark.sql.functions import avg  

# Grouping and aggregating  
df.groupBy("Name").count().show()  

# Calculating average  
df.agg(avg("Age")).show()

Joins

data2 = [{"Name": "Alice", "City": "New York"}, {"Name": "Bob", "City": "San Francisco"}]  
df2 = spark.createDataFrame(data2)  

# Inner join  
joined = df.join(df2, on="Name", how="inner")  
joined.show()

5๏ธโƒฃ Advantages of DataFrames

  • Performance: DataFrames benefit from Sparkโ€™s Catalyst Optimizer, improving execution speed.
  • Ease of Use: SQL-like syntax makes them beginner-friendly.
  • Compatibility: Supports reading/writing in multiple formats like JSON, Parquet, and CSV.
  • API Richness: Provides a vast array of functions for data transformation and analysis.

6๏ธโƒฃ Frequently Asked Interview Questions ๐Ÿค”

  1. What is the difference between RDDs, DataFrames, and Datasets in Spark?
  2. Why are DataFrames faster than RDDs?
  3. How can you create a DataFrame from an RDD in Spark?
  4. Explain the Catalyst Optimizer in Spark.
  5. What are the key advantages of DataFrames over RDDs?
  6. How do you perform a join operation in Spark DataFrames?
  7. What is the significance of inferSchema in Spark?
  8. How do you execute SQL queries on Spark DataFrames?

By mastering Spark DataFrames, you unlock the ability to process and analyze structured data efficiently. Whether youโ€™re transitioning from RDDs or learning Spark for the first time, DataFrames are the perfect starting point.

๐Ÿ’ฌ Whatโ€™s your experience working with Spark DataFrames? Share your insights in the comments below! ๐Ÿš€


๋ฉ”ํƒ€๋ฐ์ดํ„ฐ
post_id
d6af7bf28d89
slug
working-with-spark-dataframes-a-better-way-to-handle-data-d6af7bf28d89
url
https://medium.com/@krishnatej2207/working-with-spark-dataframes-a-better-way-to-handle-data-d6af7bf28d89
canonical_url
https://medium.com/@krishnatej2207/working-with-spark-dataframes-a-better-way-to-handle-data-d6af7bf28d89
author_url
https://medium.com/@krishnatej2207
status
ok
fetched_at
2026-07-24 04:20:45