Built with Tutify.io

    AIStudyLabs

    Explain PySpark round() Function

    Explain PySpark round() Function

    In PySpark, the round() function is commonly used to round numeric columns to a specified number of decimal places. It is a part of pyspark.sql.functions and works on numeric data types such as IntegerType, FloatType, and DoubleType.

    This function is particularly useful for formatting numerical values for reporting, calculations, and standardizing precision in DataFrames.

    In this article, I will cover the different ways to use the round() function in PySpark.

    Key Points-

    <a id="h-pyspark-round-function"></a>

    PySpark round() Function

    The round() function is used to round a numeric column to the specified number of decimal places.

    <a id="h-syntax"></a>

    Syntax

    The following is the syntax of the round() function.

    
    # Syntax of round()
    pyspark.sql.functions.round(col, scale=0)
    

    <a id="h-parameters"></a>

    Parameters

    <a id="h-return-value"></a>

    Return Value

    Returns a column of the same numeric type with rounded values.

    <a id="h-pyspark-round-to-2-decimal-places"></a>

    PySpark Round to 2 Decimal Places

    You can round a numeric column to exactly two decimal places by passing scale=2 to the round() function.
    This will return the same values but restricted to two digits after the decimal point.

    
    # Round to 2 decimal places
    from pyspark.sql import SparkSession
    from pyspark.sql.functions import round, col
    
    # Create SparkSession
    spark = SparkSession.builder.appName("aistudylabs").getOrCreate()
    
    # Sample Data
    data = [(1, 123.4567), (2, 78.1234), (3, 45.9876)]
    columns = ["id", "amount"]
    df = spark.createDataFrame(data, columns)
    
    # Round to 2 decimal places
    df1 = df.withColumn("amount_2dec", round(col("amount"), 2))
    df1.show()
    

    Yields below the output.

    <a id="h-pyspark-round-up"></a>

    PySpark Round Up

    If you want to always round up to the nearest integer, you can use the ceil() function instead of round(). This will return numbers increased to the next whole number, even if the decimal part is small.

    
    # round up to the nearest integer
    from pyspark.sql.functions import ceil
    
    df1 = df.withColumn("amount_roundup", ceil(col("amount")))
    df1.show()
    

    Yields below the output.

    <a id="h-pyspark-round-to-nearest-integer"></a>

    PySpark Round to Nearest Integer

    By default, you can round numbers to the nearest whole number without specifying a scale. This will return values as integers, dropping any decimal part.

    
    # Round to Nearest Integer
    df1 = df.withColumn("amount_int", round(col("amount")))
    df1.show()
    

    Yields below the output.

    
    # Output:
    +---+--------+----------+
    | id|  amount|amount_int|
    +---+--------+----------+
    |  1|123.4567|     123.0|
    |  2| 78.1234|      78.0|
    |  3| 45.9876|      46.0|
    +---+--------+----------+
    

    <a id="h-pyspark-round-column"></a>

    PySpark Round Column

    You can round a specific column to a chosen number of decimal places using withColumn(). This will return the original values but with the selected column rounded as specified.

    
    df1 = df.withColumn("rounded_amount", round(col("amount"), 1))
    df1.show()
    

    Yields below the output.

    
    # Output:
    +---+--------+--------------+
    | id|  amount|rounded_amount|
    +---+--------+--------------+
    |  1|123.4567|         123.5|
    |  2| 78.1234|          78.1|
    |  3| 45.9876|          46.0|
    +---+--------+--------------+
    

    <a id="h-round-all-columns"></a>

    Round All Columns

    To round all numeric columns in a DataFrame, you can use list comprehension with select() function. This will return the DataFrame with every float or double column rounded to the given scale.

    
    # Round all numeric columns to 2 decimals
    df1 = df.select(*[
        round(col(c), 2).alias(c) if df.schema[c].dataType.typeName() in ["double", "float"] else col(c)
        for c in df.columns
    ])
    df1.show()
    

    Yields below the output.

    
    # Output:
    +---+------+
    | id|amount|
    +---+------+
    |  1|123.46|
    |  2| 78.12|
    |  3| 45.99|
    +---+------+
    

    <a id="h-pyspark-round-float"></a>

    PySpark Round Float

    You can round both float and double-precision values in the same way using the round() function. This will return floating-point numbers limited to the defined decimal places.

    
    # Round float values
    float_data = [(1.2345,), (2.6789,), (3.14159,)]
    float_df = spark.createDataFrame(float_data, ["value"])
    float_df.withColumn("rounded", round(col("value"), 2)).show()
    

    Yields below the output.

    
    # Output:
    +-------+-------+
    |  value|rounded|
    +-------+-------+
    | 1.2345|   1.23|
    | 2.6789|   2.68|
    |3.14159|   3.14|
    +-------+-------+
    

    <a id="h-pyspark-round-not-working"></a>

    PySpark Round Not Working

    You can sometimes notice results that don’t match expectations when rounding values ending in .5.
    This happens because PySpark uses banker’s rounding (round half to even), which may return even numbers instead of always rounding up.

    
    # Example of banker’s rounding
    df1 = spark.createDataFrame([(2.5,), (3.5,), (4.5,)], ["value"])
    df1.withColumn("rounded", round(col("value"))).show()
    

    Yields below the output.

    
    # Output:
    +-----+-------+
    |value|rounded|
    +-----+-------+
    |  2.5|    3.0|
    |  3.5|    4.0|
    |  4.5|    5.0|
    +-----+-------+
    

    This happens because PySpark uses banker’s rounding (round half to even), not always rounding halves up.

    <a id="h-use-bround-for-round-half-up-behavior"></a>

    Use bround() for round half up behavior

    To achieve round-half-up behavior instead of banker’s rounding, you can use the bround() function.
    This will return .5 values rounded away from zero, as many users expect.

    
    # Use bround() for round half up
    from pyspark.sql.functions import bround
    df1.withColumn("rounded_halfup", bround(col("value"))).show()
    
    
    +-----+--------------+
    |value|rounded_halfup|
    +-----+--------------+
    |  2.5|           2.0|
    |  3.5|           4.0|
    |  4.5|           4.0|
    +-----+--------------+
    

    <a id="h-frequently-asked-questions-of-round"></a>

    Frequently Asked Questions of round()

    Use round(col("column_name"), 2) to round values to 2 decimal places

    round(): Banker’s rounding (round half to even).

    Because PySpark uses banker’s rounding, where 0.5 values are rounded to the nearest even number.

    Use the ceil() function to always round up.

    It works with numeric columns (float, double) but not on non-numeric types.

    By using select() with list comprehension to apply round() to all numeric columns.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, we explored the PySpark round() function, which is widely used for handling numeric precision.

    With these techniques, you can handle numeric rounding in PySpark efficiently.

    Happy Learning!!

    <a id="h-reference"></a>

    Reference