Round all columns in dataframe - two decimal place pyspark

Viewed 7865

I have this command for all columns in my dataframe to round to 2 decimal places:

data = data.withColumn("columnName1", func.round(data["columnName1"], 2))

I have no idea how to round all Dataframe by the one command (not every column separate). Could somebody help me, please? I don't want to have the same command 50times with different column name.

1 Answers

There is not a function or command for applying all functions to the columns but you can iterate.

+-----+-----+
| col1| col2|
+-----+-----+
|1.111|2.222|
+-----+-----+

df = spark.read.option("header","true").option("inferSchema","true").csv("test.csv")

for c in df.columns:
    df = df.withColumn(c, round(c, 2))
    
df.show()

+----+----+
|col1|col2|
+----+----+
|1.11|2.22|
+----+----+
Related