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 | first 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's SQL function first(~) method returns the first value of the specified column of a PySpark DataFrame.

Parameters

1. col | string or Column object

The column label or Column object of interest.

2. ignorenulls | boolean | optional

Whether or not to ignore null values. By default, ignorenulls=False.

Return Value

A PySpark SQL Column object (pyspark.sql.column.Column).

Examples

Consider the following PySpark DataFrame:

columns = ["name", "age"]
data = [("Alex", 15), ("Bob", 20), ("Cathy", 25)]
df = spark.createDataFrame(data, columns)
df.show()
+-----+---+
| name|age|
+-----+---+
| Alex| 15|
| Bob| 20|
|Cathy| 25|
+-----+---+

Getting the first value of a column in PySpark DataFrame

To get the first value of the name column:

import pyspark.sql.functions as F
df.select(F.first(df.name)).show()
+-----------+
|first(name)|
+-----------+
| Alex|
+-----------+

Getting the first non-null value of a column in PySpark DataFrame

Consider the following PySpark DataFrame with null values:

columns = ["name", "age"]
data = [("Alex", None), ("Bob", 20), ("Cathy", 25)]
df = spark.createDataFrame(data, columns)
df.show()
+-----+----+
| name| age|
+-----+----+
| Alex|null|
| Bob| 20|
|Cathy| 25|
+-----+----+

By default, ignorenulls=False, which means that the first value is returned regardless of whether it is null or not:

df.select(F.first(df.age)).show()
+----------+
|first(age)|
+----------+
| null|
+----------+

To return the first non-null value instead:

df.select(F.first(df.age, ignorenulls=True)).show()
+----------+
|first(age)|
+----------+
| 20|
+----------+

Getting the first value of each group in PySpark

The first(~) method is also useful in aggregations. Consider the following PySpark DataFrame:

columns = ["name", "class"]
data = [("Alex", "A"), ("Alex", "B"), ("Bob", None), ("Bob", "A"), ("Cathy", "C")]
df = spark.createDataFrame(data, columns)
df.show()
+-----+-----+
| name|class|
+-----+-----+
| Alex| A|
| Alex| B|
| Bob| null|
| Bob| A|
|Cathy| C|
+-----+-----+

To get the first value of each aggregate:

df.groupby("name").agg(F.first("class")).show()
+-----+------------+
| name|first(class)|
+-----+------------+
| Alex| A|
| Bob| null|
|Cathy| C|
+-----+------------+

Here, we are grouping by name, and then for each of these group, we are obtaining the first value that occurred in the class column.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down