PySpark function that is used to convert angle measures from degrees to radians.

PySpark @ Freshers.in

Within its extensive library of functions, radians plays a crucial role for users dealing with trigonometric operations. The radians function in PySpark is used to convert angle measures from degrees to radians. This conversion is essential in various scientific calculations, especially in fields involving geometry or physics. This article provides an in-depth look at the radians function, enhanced by a practical example.

Syntax:

from pyspark.sql.functions import radians
df.withColumn("radians_column", radians(df["degrees_column"]))

Example

Consider a scenario where we have a dataset containing angles in degrees, and we need to convert these angles to radians for further trigonometric calculations.

Sample data

Suppose we have the following data in a DataFrame named angles_df:

AngleName Degrees
Angle A 30
Angle B 45
Angle C 60
Angle D 90
Angle E 180

Code implementation

from pyspark.sql import SparkSession
from pyspark.sql.functions import radians
from pyspark.sql.types import *
# Initialize Spark Session
spark = SparkSession.builder.appName("Radians Example at Freshers.in").getOrCreate()
# Sample data
data = [("Angle A", 30),
        ("Angle B", 45),
        ("Angle C", 60),
        ("Angle D", 90),
        ("Angle E", 180)]
# Define schema
schema = StructType([
    StructField("AngleName", StringType(), True),
    StructField("Degrees", IntegerType(), True)
])
# Create DataFrame
angles_df = spark.createDataFrame(data, schema)
# Apply radians function
angles_df_in_radians = angles_df.withColumn("Radians", radians(angles_df["Degrees"]))
# Show results
angles_df_in_radians.show()

The resulting DataFrame will include the original angles in degrees alongside a new column, Radians, displaying the converted values in radians. This conversion facilitates further trigonometric analysis.

+---------+-------+------------------+
|AngleName|Degrees|           Radians|
+---------+-------+------------------+
|  Angle A|     30|0.5235987755982988|
|  Angle B|     45|0.7853981633974483|
|  Angle C|     60|1.0471975511965976|
|  Angle D|     90|1.5707963267948966|
|  Angle E|    180| 3.141592653589793|
+---------+-------+------------------+
Author: user