VOOZH about

URL: https://www.analyticsvidhya.com/blog/2022/10/most-important-pyspark-functions-with-example/

โ‡ฑ Most Important PySpark Functions with Example - Analytics Vidhya


India's Most Futuristic AI Conference Is Back โ€“ Bigger, Sharper, Bolder

  • d
  • :
  • h
  • :
  • m
  • :
  • s

Most Important PySpark Functions with Example

Kishan Yadav Last Updated : 22 Oct, 2022
6 min read

This article was published as a part of the Data Science Blogathon.

Introduction

The Python API for Apache Spark is known as PySpark.To develop spark applications in Python, we will use PySpark. It also provides the Pyspark shell for real-time data analysis. PySpark supports most of the Apache Spark functionality, including Spark Core, SparkSQL, DataFrame, Streaming, MLlib (Machine Learning), and MLlib (Machine Learning).

This article will explore useful PySpark functions with scenario-based examples to understand them better.

The expr() function

It is a SQL function in PySpark to ๐ž๐ฑ๐ž๐œ๐ฎ๐ญ๐ž ๐’๐๐‹-๐ฅ๐ข๐ค๐ž ๐ž๐ฑ๐ฉ๐ซ๐ž๐ฌ๐ฌ๐ข๐จ๐ง๐ฌ. It will accept a SQL expression as a string argument and execute the commands written in the statement. It enables the use of SQL-like functions that are absent from the PySpark Column type and pyspark.sql.functions API. Ex:- ๐‚๐€๐’๐„ ๐–๐‡๐„๐. We are allowed to use ๐ƒ๐š๐ญ๐š๐…๐ซ๐š๐ฆ๐ž ๐œ๐จ๐ฅ๐ฎ๐ฆ๐ง๐ฌ in the expression. The syntax for this function is ๐ž๐ฑ๐ฉ๐ซ(๐ฌ๐ญ๐ซ).

# importing necessary libs
from pyspark.sql import SparkSession
from pyspark.sql.functions import expr
# creating session
spark = SparkSession.builder.appName("practice").getOrCreate()
# create data
data = [("Prashant","Banglore",25, 58, "2022-08-01", 1),
 ("Ankit","Banglore",26,54,"2021-05-02", 2),
 ("Ramakant","Gurugram",24, 60, "2022-06-02", 3),
 ("Brijesh","Gazipur", 26,75,"2022-07-04", 4),
 ("Devendra","Gurugram", 27, 62, "2022-04-03", 5),
 ("Ajay","Chandigarh", 25,72,"2022-02-01", 6)]
columns= ["friends_name","location", "age", "weight", "meetup_date", "offset"]
df_friends = spark.createDataFrame(data = data, schema = columns)
df_friends.show()

Letโ€™s see the practical Implementations:-

Example:- A.) Concatenating one or more columns using expr()

# concate friend's name, age, and location columns using expr()
df_concat = df_friends.withColumn("name-age-location",expr("friends_name|| '-'|| age || '-' || location"))
df_concat.show()

We have joined the name, age, and location columns and stored the result in a new column called โ€œname-age-location.โ€

Example:- B.) Add a new column based on a condition (CASE WHEN) using expr() 

# check if exercise needed based on weight
# if weight is more or equal to 60 -- Yes
# if weight is less than 55 -- No
# else -- Enjoy
df_condition = df_friends.withColumn("Exercise_Need", expr("CASE WHEN weight >= 60 THEN 'Yes' " + "WHEN weight < 55 THEN 'No' ELSE 'Enjoy' END"))
df_condition.show()

Our โ€œExercise_Needโ€ column received three values (Enjoy, No, and Yes) based on the condition given in CASE WHEN. The first value of the weight column is 58, so itโ€™s less than 60 and more than 55, so the result is โ€œEnjoy.โ€

Example:- C.) Creating a new column using the current column value inside the expression.

# let increment the meetup month by the number of offset
df_meetup = df_friends.withColumn("new_meetup_date", expr("add_months(meetup_date,offset)"))
df_meetup.show()

The โ€œmeetup_dateโ€ month value increases by the offset value, and the newly generated result is stored in the โ€œnew_meetup_dateโ€ column.

The Padding Functions

A.) lpad():- 

This function provides padding to the left side of the column, and the inputs for this function are column name, length, and padding string.

B.) rpad ():- 

This function is used to add padding to the right side of the column. Column name, length, and padding string are additional inputs for this function.

Note:- 

  • If the column value is longer than the specified length, the return value will be shortened to length characters or bytes.
  • If the padding value is not specified, then the column value will be padded to the left or right depending on the function you are using, with space characters if it is a character string and with zeros if it is a byte sequence.

Letโ€™s first create a data Frame:-

# importing necessary libs
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, lpad, rpad

# creating session
spark = SparkSession.builder.appName("practice").getOrCreate()

# creating data
data = [("Delhi",30000),("Mumbai",50000),("Gujrat",80000)]
columns= ["state_name","state_population"]
df_states = spark.createDataFrame(data = data, schema = columns)
df_states.show()

Example:- 01 โ€“ Use of left padding

# left padding
df_states = df_states.withColumn('states_name_leftpad', lpad(col("state_name"), 10, '#'))
df_states.show(truncate =False)

We added the โ€˜#โ€™ symbol to the left of the โ€œstate_nameโ€ column values, and the total length of column values becomes โ€œ10โ€ณ after the padding.

Example:-02 โ€“ Right padding

# right padding
df_states = df_states.withColumn('states_name_rightpad', rpad(col("state_name"), 10, '#'))
df_states.show(truncate =False)

We added the โ€œ#โ€ symbol to the right of the โ€œstate_nameโ€ column values, and the total length becomes ten after the right padding.

Example:-03 โ€“ When the column string length is longer than the padded string length

df_states = df_states.withColumn('states_name_condition', lpad(col("state_name"), 3, '#'))
df_states.show(truncate =False)

In this case, the return column value will be shortened to the length of the padded string length. You can see the โ€œstate_name_conditionโ€ column only has values of length 3, which is the padded length we have given in the function.

The repeat() Function

In PySpark, we use the repeat function to duplicate the column values. The repeat(str,n) function returns the string containing the specified string value repeated n times.

Example:- 01 

# importing necessary libs
from pyspark.sql import SparkSession
from pyspark.sql.functions import expr, repeat

# creating session
spark = SparkSession.builder.appName("practice").getOrCreate()

# # create data
data = [("Prashant",25, 80), ("Ankit",26, 90),("Ramakant", 24, 85)]
columns= ["student_name", "student_age", "student_score"]
df_students = spark.createDataFrame(data = data, schema = columns)
df_students.show()

# repeating the column (student_name) twice and saving results in new column
df_repeated = df_students.withColumn("student_name_repeated",(expr("repeat(student_name, 2)")))
df_repeated.show()

We have repeated the โ€œstudent_nameโ€ column values in the above example twice.

We can also use this function with the Concat function, where we can repeat some string values n times before column values, working like padding, where n may be the length of some values.

The startswith() and endswith() function

startswith():-

It will produce a boolean result of True or False. When the Dataframe column value ends with the string provided as a parameter to this method, it returns True. If no match is found, it returns False.

endswith():-

The boolean value (True/False) will be returned. When the DataFrame column value ends with a string supplied as an input to this method, it returns True. False is returned if not matched.

Note:-

  • Return ๐๐”๐‹๐‹ if either of the column values or input strings are ๐๐”๐‹๐‹.
  • Return ๐—ง๐—ฟ๐˜‚๐—ฒ if the input check strings are empty.
  • These methods are case-sensitive.

Create a data frame:-

# importing necessary libs
from pyspark.sql import SparkSession
from pyspark.sql.functions import col

# creating session
spark = SparkSession.builder.appName("practice").getOrCreate()

# # create dataframe
data = [("Prashant",25, 80), ("Ankit",26, 90),("Ramakant", 24, 85), (None, 23, 87)]
columns= ["student_name", "student_age", "student_score"]
df_students = spark.createDataFrame(data = data, schema = columns)
df_students.show()

Example โ€“ 01  First, check the output type.

df_internal_res = df_students.select(col("student_name").endswith("it").alias("internal_bool_val"))
df_internal_res.show()
  • The output is a boolean value.
  • The output value is null for the last row value because the corresponding value of the โ€œstudents_nameโ€ column is NULL.

Example โ€“ 02

  • Now we use the filter() method to fetch the rows corresponding to the True values.
df_check_start = df_students.filter(col("student_name").startswith("Pra"))
df_check_start.show()

Here we got the first row as output because the โ€œstudent_nameโ€ column value starts with the value mentioned inside the function.

Example โ€“ 03 

df_check_end = df_students.filter(col("student_name").endswith("ant"))
df_check_end.show()

Here we got the two rows as output because the โ€œstudent_nameโ€ column value ends with the value mentioned inside the function.

Example โ€“ 04 โ€“ What if arguments in functions are empty?

df_check_empty = df_students.filter(col("student_name").endswith(""))
df_check_empty.show()

In this case, we get a True value corresponding to each row, and no False value returned.

Conclusion

In this article, we started our discussion by defining PySpark and its features. Then we talk about functions, their definitions, and their syntax. After discussing each function, we created a data frame and practiced some examples using it. We covered six functions in this article.

Key takeaways from this article are:-

  • I use the โ€œexprโ€œ function to concatenate columns with SQL-like expressions in PySpark.
  • We passed the columnโ€™s name as a string in the above function.
  • Creating a new column using the column value inside the expression.
  • Add padding to the column values.
  • Repeat the column values multiple times using the repeat function.
  • We also checked whether column values start or end with a particular word or not.

I hope this article helps you to understand the PySpark functions. If you have any opinions or questions, then comment down below. Connect with me on LinkedIn for further discussion.

Keep Learning!!!

The media shown in this article is not owned by Analytics Vidhya and is used at the Authorโ€™s discretion.

Hello ๐Ÿ‘‹,
I am a Data Engineer with a proven track record of working in the information technology and services industry. I am skilled in Apache Spark, Hive, SQL, Python, Hadoop, Databricks and Cloud.

Login to continue reading and enjoy expert-curated content.

Free Courses

Generative AI - A Way of Life

Explore Generative AI for beginners: create text and images, use top AI tools, learn practical skills, and ethics.

Getting Started with Large Language Models

Master Large Language Models (LLMs) with this course, offering clear guidance in NLP and model training made simple.

Building LLM Applications using Prompt Engineering

This free course guides you on building LLM apps, mastering prompt engineering, and developing chatbots with enterprise data.

Improving Real World RAG Systems: Key Challenges & Practical Solutions

Explore practical solutions, advanced retrieval strategies, and agentic RAG systems to improve context, relevance, and accuracy in AI-driven applications.

Microsoft Excel: Formulas & Functions

Master MS Excel for data analysis with key formulas, functions, and LookUp tools in this comprehensive course.

Responses From Readers

Flagship Programs

GenAI Pinnacle Program| GenAI Pinnacle Plus Program| AI/ML BlackBelt Program| Agentic AI Pioneer Program

Free Courses

Generative AI| DeepSeek| OpenAI Agent SDK| LLM Applications using Prompt Engineering| DeepSeek from Scratch| Stability.AI| SSM & MAMBA| RAG Systems using LlamaIndex| Building LLMs for Code| Python| Microsoft Excel| Machine Learning| Deep Learning| Mastering Multimodal RAG| Introduction to Transformer Model| Bagging & Boosting| Loan Prediction| Time Series Forecasting| Tableau| Business Analytics| Vibe Coding in Windsurf| Model Deployment using FastAPI| Building Data Analyst AI Agent| Getting started with OpenAI o3-mini| Introduction to Transformers and Attention Mechanisms

Popular Categories

AI Agents| Generative AI| Prompt Engineering| Generative AI Application| News| Technical Guides| AI Tools| Interview Preparation| Research Papers| Success Stories| Quiz| Use Cases| Listicles

Generative AI Tools and Techniques

GANs| VAEs| Transformers| StyleGAN| Pix2Pix| Autoencoders| GPT| BERT| Word2Vec| LSTM| Attention Mechanisms| Diffusion Models| LLMs| SLMs| Encoder Decoder Models| Prompt Engineering| LangChain| LlamaIndex| RAG| Fine-tuning| LangChain AI Agent| Multimodal Models| RNNs| DCGAN| ProGAN| Text-to-Image Models| DDPM| Document Question Answering| Imagen| T5 (Text-to-Text Transfer Transformer)| Seq2seq Models| WaveNet| Attention Is All You Need (Transformer Architecture) | WindSurf| Cursor

Popular GenAI Models

Llama 4| Llama 3.1| GPT 4.5| GPT 4.1| GPT 4o| o3-mini| Sora| DeepSeek R1| DeepSeek V3| Janus Pro| Veo 2| Gemini 2.5 Pro| Gemini 2.0| Gemma 3| Claude Sonnet 3.7| Claude 3.5 Sonnet| Phi 4| Phi 3.5| Mistral Small 3.1| Mistral NeMo| Mistral-7b| Bedrock| Vertex AI| Qwen QwQ 32B| Qwen 2| Qwen 2.5 VL| Qwen Chat| Grok 3

AI Development Frameworks

n8n| LangChain| Agent SDK| A2A by Google| SmolAgents| LangGraph| CrewAI| Agno| LangFlow| AutoGen| LlamaIndex| Swarm| AutoGPT

Data Science Tools and Techniques

Python| R| SQL| Jupyter Notebooks| TensorFlow| Scikit-learn| PyTorch| Tableau| Apache Spark| Matplotlib| Seaborn| Pandas| Hadoop| Docker| Git| Keras| Apache Kafka| AWS| NLP| Random Forest| Computer Vision| Data Visualization| Data Exploration| Big Data| Common Machine Learning Algorithms| Machine Learning| Google Data Science Agent
๐Ÿ‘ Av Logo White

Continue your learning for FREE

Forgot your password?
๐Ÿ‘ Av Logo White

Enter OTP sent to

Edit

Wrong OTP.

Enter the OTP

Resend OTP

Resend OTP in 45s

๐Ÿ‘ Popup Banner
๐Ÿ‘ AI Popup Banner