How do I limit the number of digits after decimal point? I have a pyspark dataframe.
Month Month_start Month_end Result
2/1/2021 2349 456 515.131579086421
2/1/2021 3109 3500 88.8285714123568
3/1/2021 45.23 23.77 190.281868569833
3/1/2021 598412500 3796000.00 157.642913593256
I need to retain only 2 digits after the decimal point in the Result column and add % symbol for all values in Result column. Month, Month_start, Month_end columns are of string type and Result is of double datatype.
Here's what I tried
df=spark.read.csv(src_path, header=True, encoding='ISO-8859-1')
df=df.withColumn(format('Result', (col('Month_start')/col('Month_end')*100)),"%.2f").lit("%")
Im getting this error message
AttributeError: 'DataFrame' object has no attribute 'lit'
My expected dataframe is:
Month Month_start Month_end Result
2/1/2021 2349 456 515.13%
2/1/2021 3109 3500 88.82%
3/1/2021 45.23 23.77 190.28%
3/1/2021 598412500 3796000.00 157.64%