Adding White Spaces to Data in Spark Dataframe

Updated On February 11, 2021 | By Mahesh Mogal

You may have a use-case where you want to make value in column either string or number to have the same length. we can use "lpad" and "rpad" functions to format strings & numbers properly.

For example, you might need numbers to have the same number of digits like for month should have 2 digits and add 0 if the month has only one digit.


lpad function is used to add padding from the left side to string or number. This is useful in the example mentioned above where we would like to add 0 to the left of the month if it has one digit only.

lpad example
Lpad Example

In the above example, we have added 0 to the left side of the number to make it of 4 digits long in each case.


In same way we can use rpad to add digits to right side of string or number.

Rpad Example

I hope you found this useful. If you have any questions do let me know. See you later.


Mahesh Mogal

I am passionate about Cloud, Data Analytics, Machine Learning, and Artificial Intelligence. I like to learn and try out new things. I have started blogging about my experience while learning these exciting technologies.

Stay Updated with Latest Blogs

Get latest blogs delivered to your mail directly.

Recent Posts

Where and Filter in Spark Dataframes

In this blog, we will learn how to filter rows from spark dataframe using Where and Filter functions.

Where and Filter in Spark Dataframes
Read More
Distinct Rows and Distinct Count from Spark Dataframe

Getting distinct values from columns or rows is one of most used operations. We will learn how to get distinct values as well as count of distinct values.

Distinct Rows and Distinct Count from Spark Dataframe
Read More
Sorting in Spark Dataframe

In this blog, we will learn how to sort rows in spark dataframe based on some column values.

Sorting in Spark Dataframe
Read More

Leave a Reply

Your email address will not be published. Required fields are marked *

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram
Share via
Copy link
Powered by Social Snap