
cos()
The cos()
function calculates the cosine of each numeric value in a column. This function is essential in scenarios involving trigonometric calculations, such as in engineering or scientific data analysis.
Usage
cos()
is applied to a column containing numeric values representing angles in radians.- It computes the cosine of each value in the column.
Create Spark Session and sample DataFrame
from pyspark.sql import SparkSessionfrom pyspark.sql.functions import cos, radians
# Initialize Spark Sessionspark = SparkSession.builder.appName("cosExample").getOrCreate()
# Sample DataFramedata = [(0,), (30,), (45,), (60,), (90,)]columns = ["Angle (Degrees)"]df = spark.createDataFrame(data, columns)df.show()
Output:
+---------------+
|Angle (Degrees)|
+---------------+
| 0|
| 30|
| 45|
| 60|
| 90|
+---------------+
Example: Use cos()
to calculate cosine value
- The DataFrame df contains angles in degrees.
radians("Angle (Degrees)")
: Converts the angle from degrees to radians.cos(...)
: Calculates the cosine of each angle (now in radians). The result is aliased as Cosine for clarity.
cos_df = df.select(df["Angle (Degrees)"], cos(radians("Angle (Degrees)")).alias("Cosine"))cos_df.show()
Output:
+---------------+--------------------+
|Angle (Degrees)| Cosine|
+---------------+--------------------+
| 0| 1.0|
| 30| 0.8660254037844387|
| 45| 0.7071067811865476|
| 60| 0.5000000000000001|
| 90|6.123233995736766...|
+---------------+--------------------+
# Stop the Spark Sessionspark.stop()