
ceil()
The ceil()
function is used to round up numeric values to the nearest integer.
Usage
ceil()
takes a column with numeric values and applies the mathematical ceiling function to each value, resulting in integers.- It is particularly useful when you need to ensure that values are rounded up in calculations.
Create Spark Session and sample DataFrame
from pyspark.sql import SparkSessionfrom pyspark.sql.functions import ceil
# Initialize Spark Sessionspark = SparkSession.builder.appName("ceilExample").getOrCreate()
# Sample DataFramedata = [(2.5,), (3.1,), (4.8,), (5.0,)]columns = ["Value"]df = spark.createDataFrame(data, columns)df.show()
Output:
+-----+
|Value|
+-----+
| 2.5|
| 3.1|
| 4.8|
| 5.0|
+-----+
Example: Use ceil()
to round up numerical values to integers
ceil("Value")
: round up each value in the Value column to their nearest ceiling integer.alias("Ceiling Value")
: It renames the resulted new column as Ceiling Value.
ceil_df = df.select(ceil("Value").alias("Ceiling Value"))ceil_df.show()
Output:
+-------------+
|Ceiling Value|
+-------------+
| 3|
| 4|
| 5|
| 5|
+-------------+
# Stop the Spark Sessionspark.stop()