For Loop in PySpark/Python with Example of DataFrame

Creating a Simple DataFrame

To get started with PySpark DataFrames, let's begin by creating a simple DataFrame using the PySpark library. DataFrames are similar to tables in a relational database or spreadsheets, providing a structured and organized way to work with data.

Steps to Create a Simple DataFrame:

  1. Import Required Libraries:
       from pyspark.sql import SparkSession
       # Create a Spark session
       spark = SparkSession.builder.appName("SimpleDataFrame").getOrCreate()
  3. Define Sample Data:
       data = [("Alice", 25), ("Bob", 30), ("Charlie", 28)]
       columns = ["Name", "Age"]
  5. Create the DataFrame:
       df = spark.createDataFrame(data, columns)
  7. Show the DataFrame:
  9. Stop the Spark Session:

By following these steps, you can easily create a simple DataFrame using PySpark. DataFrames provide a powerful way to manage and manipulate structured data within your Spark applications.

Viewing Data

Once you have created a DataFrame, you might want to explore and view its contents. PySpark provides various methods to help you examine the data within a DataFrame.

Viewing DataFrame Contents:

  1. Using show():

    You can also specify the number of rows to display:

  3. Using head():
       rows = df.head(5)  
       for row in rows:
  5. Using take():
       rows = df.take(5)  
       for row in rows:
  7. Using collect():
       rows = df.collect()  
       for row in rows:

By using these methods, you can easily view the contents of a DataFrame and gain insights into your data.

Looping Through Rows with a for Loop

In data analysis, you often need to perform operations on each row of a DataFrame. PySpark allows you to iterate through the rows of a DataFrame using a for loop.

Example: Processing DataFrame Rows Using a for Loop

Suppose we have a DataFrame containing information about individuals and their ages. We want to categorize each person as "Young" or "Old" based on their age.

Here's how you can achieve this using a for loop:

def process_row(row):
    name = row["Name"]
    age = row["Age"]
    if age < 30:
        category = "Young"
        category = "Old"
    return name, age, category

result_list = []

for row in df.collect():

result_columns = ["Name", "Age", "Category"]
result_df = spark.createDataFrame(result_list, result_columns)

In this example, we define a function process_row() that takes a row as input, extracts the name and age columns, and categorizes the person based on their age. We then iterate through each row in the original DataFrame using a for loop, apply the processing function to each row, and store the results in a new DataFrame.

This approach allows you to perform custom operations on each row of a DataFrame using a for loop and create a new DataFrame containing the processed data.


In this tutorial, we covered the basics of creating DataFrames in PySpark, viewing DataFrame contents, and using for loops to iterate through DataFrame rows. These concepts form the foundation of data manipulation and analysis using PySpark DataFrames. With a solid understanding of these concepts, you can start exploring more advanced topics and techniques for working with data in PySpark.



Contact Form