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 Column | getItem 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 Column's getItem(~) method extracts a value from the lists or dictionaries in a PySpark Column.

Parameters

1. key | any

The key value depends on the column type:

  • for lists, key should be an integer index indicating the position of the value that you wish to extract.

  • for dictionaries, key should be the key of the values you wish to extract.

Return Value

A new PySpark Column.

Examples

Consider the following PySpark DataFrame:

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

Extracting n-th item in lists

To extract the second value from each list in the vals column:

# Assign a label to the column returned by getItem(~)
df_result = df.select(F.col('vals').getItem(1).alias('2nd val'))
df_result.show()
+-------+
|2nd val|
+-------+
| 6|
| 8|
+-------+

Note that we could also use [~] syntax instead of getItem(~):

df_result = df.select(F.col('vals')[1].alias('2nd val'))
df_result.show()
+-------+
|2nd val|
+-------+
| 6|
| 8|
+-------+

Specifying an index position that is out of bounds for the list will return a null value:

df_result = df.select(F.col('vals').getItem(9))
df_result.show()
+-------+
|2nd val|
+-------+
| null|
| null|
+-------+

Extracting values using keys in dictionaries

Consider the following PySpark DataFrame:

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

To extract the value where the key is 'A':

df_result = df.select(F.col('vals').getItem('A'))
df_result.show()
+-------+
|vals[A]|
+-------+
| 4|
| 5|
+-------+

Note that referring to keys that do not exist will return null:

df_result = df.select(F.col('vals').getItem('C'))
df_result.show()
+-------+
|vals[C]|
+-------+
| null|
| null|
+-------+
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
1
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!