| by Arround The Web | No comments

PySpark radians() and degrees() Functions

radians() Function

The radians() function in PySpark returns the radians of any given number present in a DataFrame column.

It can be used with the select() method because the select() function is used to display the values in the PySpark DataFrame.

Syntax

 

dataframe_obj.select(radians(dataframe_obj.column))

 
Parameter:

It takes the column name as a parameter to return radians for that column.

Example 1

Let’s create a PySpark DataFrame with 3 rows and 4 columns, plus all the numeric types and return radians.

import pyspark
import math
from pyspark.sql import SparkSession
from pyspark.sql.functions import radians
 
spark_app = SparkSession.builder.appName('_').getOrCreate()
 
 
 #create math values
values =[(math.pi,0,7.8,120),
           (math.pi/2,1,0.5,180),
           (math.pi/3,-5,-12.9,360)
              ]
 #assign columns by creating the PySpark DataFrame
dataframe_obj = spark_app.createDataFrame(values,['value1','value2','value3','value4'])
 
dataframe_obj.show()
 
 #get the radians of value1 column
dataframe_obj.select(radians(dataframe_obj.value1)).show()

 
Output:


So, for the column value1, we returned the following radians:

3.141592653589793 is equal to 0.05483113556160755 radians.

1.5707963267948966 is equal to 0.027415567780803774 radians.

1.0471975511965976 is equal to 0.018277045187202513 radians.

Example 2

Now, we will return radians for value2 and value3 columns.

import pyspark
import math
from pyspark.sql import SparkSession
from pyspark.sql.functions import radians
 
spark_app = SparkSession.builder.appName('_').getOrCreate()
 
 
 #create math values
values =[(math.pi,0,7.8,120),
           (math.pi/2,1,0.5,180),
           (math.pi/3,-5,-12.9,360)
              ]
 #assign columns by creating the PySpark DataFrame
dataframe_obj = spark_app.createDataFrame(values,['value1','value2','value3','value4'])
 
dataframe_obj.show()
 
 #get the radians values of value2 and value3 column
dataframe_obj.select(radians(dataframe_obj.value2),radians(dataframe_obj.value3)).show()

 
Output:


column – value2:

0 is equal to 0 radians.

1 is equal to 0.017453292519943295 radians.

-5 is equal to -0.08726646259971647 radians.

column – value3:

7.8 is equal to 0.1361356816555577 radians.

0.5 is equal to 0.008726646259971648 radians.

-12.9 is equal to -0.22514747350726852 radians.

degrees() Function

The degrees() function in PySpark returns the degrees of any given number present in a DataFrame column.

It can be used with the select() method because the select() function is used to display the values in the PySpark DataFrame.

Syntax

 

dataframe_obj.select(degrees(dataframe_obj.column))

 
Parameter:

It takes the column name as a parameter to return degrees for that column.

Example 1

Let’s create a PySpark DataFrame with 3 rows and 4 columns, plus all the numeric types and return degrees.

import pyspark
import math
from pyspark.sql import SparkSession
from pyspark.sql.functions import degrees
 
spark_app = SparkSession.builder.appName('_').getOrCreate()
 
 
 #create math values
values =[(math.pi,0,7.8,120),
           (math.pi/2,1,0.5,180),
           (math.pi/3,-5,-12.9,360)
              ]
 #assign columns by creating the PySpark DataFrame
dataframe_obj = spark_app.createDataFrame(values,['value1','value2','value3','value4'])
 
dataframe_obj.show()
 
 #get the degrees of value1 column
dataframe_obj.select(degrees(dataframe_obj.value1)).show()

 
Output:


So, for the column value1, we returned degrees.

3.141592653589793 is equal to 100.0 Degrees.

1.5707963267948966 is equal to 90.0 Degrees.

1.0471975511965976 is equal to 59.99999999999999 Degrees.

Example 2

Now, we will return degrees for value2 and value3 columns.

import pyspark
import math
from pyspark.sql import SparkSession
from pyspark.sql.functions import degrees
 
spark_app = SparkSession.builder.appName('_').getOrCreate()
 
 
 #create math values
values =[(math.pi,0,7.8,120),
           (math.pi/2,1,0.5,180),
           (math.pi/3,-5,-12.9,360)
              ]
 #assign columns by creating the PySpark DataFrame
dataframe_obj = spark_app.createDataFrame(values,['value1','value2','value3','value4'])
 
dataframe_obj.show()
 
 #get the degrees values of value2 and value3 column
dataframe_obj.select(degrees(dataframe_obj.value2),degrees(dataframe_obj.value3)).show()

 
Output:


column – value2:

0 is equal to 0.0 degrees.

1 is equal to 57.29577951308232 degrees.

-5 is equal to -286.4788975654116 degrees.

column – value3:

7.8 is equal to 446.9070802020421 degrees.

0.5 is equal to 28.64788975654116 degrees.

-12.9 is equal to -739.115555718762 Degrees.

Conclusion

In this PySpark tutorial, we discussed the radians() and degrees() functions. The radians() function in PySpark is used to return the radians of any given number present in a DataFrame column, and degrees() in PySpark is used to return the degrees of any given number present in a DataFrame column. We discussed two examples for both functions.

Share Button

Source: linuxhint.com

Leave a Reply