PySpark : Finding the cube root of the given value using PySpark

PySpark @

The pyspark.sql.functions.cbrt(col) function in PySpark computes the cube root of the given value. It takes a column as input and returns a new column with the cube root values.

Here’s an example to illustrate the usage of pyspark.sql.functions.cbrt(col):

To use the cbrt function in PySpark, you need to import it from the pyspark.sql.functions module. Here’s the corrected code:

from pyspark.sql import SparkSession
from pyspark.sql.functions import col, cbrt
# Create a SparkSession
spark = SparkSession.builder.getOrCreate()
# Create a DataFrame with a column of values
data = [(1,), (8,), (27,), (64,)]
df = spark.createDataFrame(data, ['value'])
# Apply the cube root transformation using cbrt() function
transformed_df = df.withColumn('cbrt_value', cbrt(col('value')))
# Show the transformed DataFrame


|    1|       1.0|
|    8|       2.0|
|   27|       3.0|
|   64|       4.0|

We import the cbrt function from pyspark.sql.functions. Then, we use the cbrt() function directly in the withColumn method to apply the cube root transformation to the ‘value’ column. The col(‘value’) expression retrieves the column ‘value’, and cbrt(col(‘value’)) computes the cube root of that column.

Now, the transformed_df DataFrame will contain the expected cube root values in the ‘cbrt_value’ column.

Spark important urls to refer

  1. Spark Examples
  2. PySpark Blogs
  3. Bigdata Blogs
  4. Spark Interview Questions
  5. Official Page
Author: user

Leave a Reply