Select Expr in Spark Dataframe

Updated On February 12, 2021 | By Mahesh Mogal

Select and Expr are one of the most used functions in the Spark dataframe. In this blog, we will learn different things that we can do with select and expr functions.

Selecting Columns from Dataframe

This is one of the most used functions for the data frame and we can use Select with "expr" to do this. Before using "expr" function we need to import it.

Operations on Column Data

A more interesting use case for "expr" is to perform different operations on column data. We can use it to get length or column, extract data, or anything which we can do it SQL.

In the above code, we are printing value in the column filed is greater than 10 or not. You can see that our column name is not very user friendly. Just like in SQL, we can give usable column names.

Using Alias with Expr

We can use Alias function to give user friendly names to columns.

Selecting All columns with expr

We might need to select other columns from the dataframe along with the newly created expression column. This is very easy. We can give a comma-separated list of columns or use "*" to list all columns from the data frame.

Selecting All columns with Expr
Selecting All Columns from Dataframe with Expr

Renaming Columns with Expr and Alias

We can use Expr to rename columns in data frame. This will actually create new columns instead of renaming old one.

SelectExpr - shorthand for select and expr

Select and Expr is so much widely used while working with Spark dataframe, that the Spark team has given shorthand to use it. We can use selectExpr function.

I hope you found this useful. See you in next blog.

Select Expr in Spark Dataframe

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