search
Search
Join our weekly DS/ML newsletter layers DS/ML Guides
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

PySpark SQL Functions | element_at method

Machine Learning
chevron_right
PySpark
chevron_right
Documentation
chevron_right
PySpark SQL Functions
schedule Jul 8, 2022
Last updated
local_offer PySpark
Tags

PySpark SQL Functions' element_at(~) method is used to extract values from lists or maps in a PySpark Column.

Parameters

1. col | string or Column

The column of lists or maps from which to extract values.

2. extraction | int

The position of the value that you wish to extract. Negative positioning is supported - extraction=-1 will extract the last element from each list.

WARNING

The position is not indexed-based. This means that extraction=1 will extract the first value in the lists or maps.

Return Value

A new PySpark Column.

Examples

Extracting n-th value from arrays in PySpark Column

Consider the following PySpark DataFrame that contains some lists:

rows = [[[5,6]], [[7,8]]]
df = spark.createDataFrame(rows, ['vals'])
df.show()
+------+
| vals|
+------+
|[5, 6]|
|[7, 8]|
+------+

To extract the second value from each list in vals, we can use element_at(~) like so:

df_res = df.select(F.element_at('vals',2).alias('2nd value'))
df_res.show()
+---------+
|2nd value|
+---------+
| 6|
| 8|
+---------+

Here, note the following:

  • the position 2 is not index-based.

  • we are using the alias(~) method to assign a label to the column returned by element_at(~).

Note that extracting values that are out of bounds will return null:

df_res = df.select(F.element_at('vals',3))
df_res.show()
+-------------------+
|element_at(vals, 3)|
+-------------------+
| null|
| null|
+-------------------+

We can also extract the last element by supplying a negative value for extraction:

df_res = df.select(F.element_at('vals',-1).alias('last value'))
df_res.show()
+----------+
|last value|
+----------+
| 6|
| 8|
+----------+

Extracting values from maps in PySpark Column

Consider the following PySpark DataFrame containing some dict values:

rows = [[{'A':4}], [{'A':5, 'B':6}]]
df = spark.createDataFrame(rows, ['vals'])
df.show()
+----------------+
| vals|
+----------------+
| {A -> 4}|
|{A -> 5, B -> 6}|
+----------------+

To extract the values that has the key 'A' in the vals column:

df_res = df.select(F.element_at('vals', F.lit('A')))
df_res.show()
+-------------------+
|element_at(vals, A)|
+-------------------+
| 4|
| 5|
+-------------------+

Note that extracting values using keys that do not exist will return null:

df_res = df.select(F.element_at('vals', F.lit('B')))
df_res.show()
+-------------------+
|element_at(vals, B)|
+-------------------+
| null|
| 6|
+-------------------+

Here, the key 'B' does not exist in the map {'A':4} so a null was returned for that row.

mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!