search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
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

PySpark SQL Functions | element_at method

schedule Aug 12, 2023
Last updated
local_offer
PySpark
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

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.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
2
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!