• Latest
  • Trending
Data Preprocessing Using PySpark’s DataFrame

Data Preprocessing Using PySpark’s DataFrame

May 6, 2022
Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023
Vice President Dr. Bawumia inaugurates  ICT Hub

Vice President Dr. Bawumia inaugurates ICT Hub

April 2, 2023
Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023
How Your Storage System Will Still Be Viable in 5 Years’ Time?

How Your Storage System Will Still Be Viable in 5 Years’ Time?

February 20, 2023
The Broken Promises From Cybersecurity Vendors

Cloud Infrastructure Used By WIP26 For Espionage Attacks on Telcos

February 20, 2023
Instagram and Facebook to get paid-for verification

Instagram and Facebook to get paid-for verification

February 20, 2023
YouTube CEO Susan Wojcicki steps down after nine years

YouTube CEO Susan Wojcicki steps down after nine years

February 20, 2023
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Thursday, 16 July, 2026
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

Data Preprocessing Using PySpark’s DataFrame

by ITECHNEWS
May 6, 2022
in Data Science, Leading Stories
0 0
0
Data Preprocessing Using PySpark’s DataFrame

Introduction on PySpark’s DataFrame

From this article, I’m starting the PySpark’s DataFrame tutorial series and this is the first arrow. In this particular article, we will be closely looking at how to get started with PySpark’s data preprocessing techniques, introducing what PySpark’s DataFrame looks like, and performing some general operations on the same i.e. from starting the PySpark’s session to dealing with data preprocessing technique using PySpark.

Starting PySpark Session

Here we will be starting our Spark session by importing it from the pyspark.sql package, and then we will setup the SparkSession by giving it a name

from pyspark.sql import SparkSession

data_spark = SparkSession.builder.appName('DataFrame_article').getOrCreate()

data_spark

Output:

PySpark Session|PySpark's DataFrame

Code breakdown

  1. Firstly we have imported the SparkSession from pyspark.sql object.
  2. Then by using getOrCreate() and builder the function, we created a SparkSession and stored it in a variable.
  3. At last, we saw what is there in the data_spark variable.

Note: This is not the detailed illustration of  ” how to start spark session”, and if you are not able to get every bit of it, then I’ll recommend going through my previous article on- Getting started with PySpark using Python

Those who already understood can jump to the main section of the article.

Reading the Dataset

Now let’s read our dataset and look at what it looks like and how PySpark reads it differently with different approaches and functions.

data_spark.read.option('header','true').csv('/content/sample_data/california_housing_train.csv')

Output:

DataFrame[longitude: string, latitude: string, housing_median_age: string, total_rooms: string, total_bedrooms: string, population: string, households: string, median_income: string, median_house_value: string]

read.option.csv: This complete set of functions is responsible for reading the CSV type of file using PySpark, where read.csv() can also work but to make the column name as the column header, we need to use option()as well

Inference: Here in the output, we can see that the DataFrame object is returned which shows the column name and corresponding type of columns.

Now let’s see the whole dataset, i.e. column and records, using the show() method.

df_spark = data_spark.read.option('header','true').csv('/content/sample_data/california_housing_train.csv').show()
df_spark

Output:

Reading the Dataset|PySpark's DataFrame

Checking DataTypes of the Columns

Now let’s check what type of data type the columns of our dataset holds and are these columns consisting anynull values or not using the printSchema() function.

df_pyspark = data_spark.read.option('header','true').csv('/content/sample_data/california_housing_train.csv')
df_pyspark.printSchema()

Output:

type of data type| PySpark's DataFrame

Inference: With the help of the print schema function, we can notice that it returned ample information related to columns and their data types.

But, Hold on! We can see that every column shows the string value, but that is not true, right? Answer: This glitch is the default setting of the print schema() function as it will always return the column type as String until we fix it.

So, Let’s fix this issue first!

df_pyspark = data_spark.read.option('header','true').csv('/content/sample_data/california_housing_train.csv', inferSchema=True)
df_pyspark.printSchema()

Output:

 

type of data type Image 1|PySpark's DataFrame

Inference: Now, we can see the valid data type corresponding to each column with just a minor change of adding one more argument  inferScehma = True Which will change the default setting of printSchema(). One more thing to keep a note is nullable = True which certainly means that the column might have null values.

There is one more way of checking the Data types of the columns which are pretty similar to what we used to do in the case of the pandas DataFrame. Let’s see that approach as well!

df_pyspark.dtypes

Output:

 

print schema| PySpark's DataFrame

Inference: Here also, it returns the same output as in the previous approach but this time in a different format as it returns the output in the form of a “list of tuples”.

Column Indexing

First, let us see how we can get the name of each column so that, based on that, we can perform our column indexing and other operations.

df_pyspark.columns

Output:

YOU MAY ALSO LIKE

French Telco Orange Hit by Cyber-Attack

ATC Ghana supports Girls-In-ICT Program

 

Column Indexing Image 1

Inference: By using the columns the object, we can see the name of all the columns present in the dataset in the list object

Now let’s understand how we can select the columns. For instance, let’s say that we want to pluck out the total_rooms column only from the dataset.

df_pyspark.select('total_rooms').show()

Output:

 

Column Indexing Image 2| PySpark's DataFrame

Inference: Here, with the help of select the function, we have selected the total_rooms column only, and it returned that column as DataFrame of PySpark.

So we have plucked out only a single column from the dataset, but what if we want to grab multiple columns. So let’s have a look at it!

df_pyspark.select(['total_rooms', 'total_bedrooms', 'median_income']).show()

Output:

Column Indexing Image 3 |PySpark's DataFrame

Inference: Now, we have passed the multiple column names in the argument of select method but in the form of list. The same logic as we used to perform in pandas DataFrame, and with just this minute change, we can grab out multiple columns from our dataset based on the requirement.

Describe Function in PySpark

Now let’s see how the describe() function works in PySpark though we also know about the panda’s describe function and the role of the PySpark’s describe() function is also the same.

df_pyspark.describe().show()

Output:

 

Describe function in PySpark| PySpark's DataFrame

Inference: So here is the result from the described function of PySpark. By looking at the output, one who is familiar with using the pandas describe function can consider it the spitting image of the pandas DataFrame because it shows the same statistics in the same way.

In this function, you can find the below-mentioned detail of the dataset:

  1. count: Where you find the total number of records present in each column.
  2. mean: Here, one can see the mean of the column values.
  3. std-dev: It will return the standard deviation of the column values.
  4. min: This will return the minimum value present in the column.
  5. max: This will produce the maximum value currently in the queue.

Adding Columns in PySpark DataFrame

Now it’s time to learn how we can create a new column in the PySpark’s Dataframe with the help of the with column() function.

df_pyspark = df_pyspark.withColumn('Updated longitude', df_pyspark['longitude']+1.2)
df_pyspark.show()

Output:

Adding columns in PySpark DataFrame | PySpark's DataFrame

Inference: From the above output, we can see that the new column is updated in the DataFrame as “Updated longitude”.

Let’s discuss what we did to add the columns:

  1. We used the withcolumn() function to add the columns or change the existing columns in the Pyspark DataFrame.
  2. Then in that function, we will be giving two parameters
    • The first one will be the name of the new column
    • The second one will be what value that new column will hold.

Dropping Columns in PySpark DataFrame

So by far, we have learned about adding the column, but here we will learn to drop specific columns because in the data preprocessing pipeline we need to drop the columns from the dataset which are not relevant to our requirements.

Dropping the column from the dataset is a pretty straightforward task, and for that, we will be using the drop() function from PySpark.

df_pyspark.drop('Updated longitude').show()

Output:

Dropping columns in PySpark DataFrame | PySpark's DataFrame

Inference: In the output, we can see that the “Updated longitude” column doesn’t exist anymore in the dataset. We noticed that we gave the column’s name in the parameter and got that column dropped from the dataset.

Note: If we want to drop multiple columns from the dataset in the same instance, we can pass the list of column names as the parameter.

Renaming the Column

After learning to add the new columns and dropping the irrelevant column, we will be looking at how to rename the columns using the withColumnRenamed() function.

df_pyspark.withColumnRenamed('population', 'population per capita').show()

Output:

Renaming the column | PySpark's DataFrame

 

Inference: From the above output, we can see that the “population” column is renamed to “population per capita” by using with columnRenamed() a function wherein one parameter we need to pass the column name to be renamed, and the following parameter will be the updated name.

Conclusion on PySpark’s DataFrame

So finally, it’s time to conclude this article and let’s quickly discuss everything that we have covered in this article with a short description of the same.

  1. The very first thing that we learned is how to start the spark session, as this is the mandatory step to go with PySpark.
  2. Then we learned how to get information regarding the dataset columns by using the printSchema() function, columns object, and describe function().
  3. Then, at last, we also looked at how to manipulate the dataset’s Schema when we saw how to add, drop and rename the columns.
Source: Aman Preet Gulati
Via: analyticsvidhya
Tags: Data Preprocessing Using PySpark’s DataFrame
ShareTweet

Get real time update about this post categories directly on your device, subscribe now.

Unsubscribe

Search

No Result
View All Result

Recent News

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025

Recent News

  • Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa July 29, 2025
  • French Telco Orange Hit by Cyber-Attack July 29, 2025
  • ATC Ghana supports Girls-In-ICT Program April 25, 2023
  • Vice President Dr. Bawumia inaugurates ICT Hub April 2, 2023
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Go to mobile version