Round double values and cast as integers

老子叫甜甜 提交于 2020-11-28 01:56:39

问题


I have a data frame in PySpark like below.

import pyspark.sql.functions as func

df = sqlContext.createDataFrame(
        [(0.0, 0.2, 3.45631),
         (0.4, 1.4, 2.82945),
         (0.5, 1.9, 7.76261),
         (0.6, 0.9, 2.76790),
         (1.2, 1.0, 9.87984)],
         ["col1", "col2", "col3"])

df.show()
+----+----+-------+ 
|col1|col2|   col3|
+----+----+-------+
| 0.0| 0.2|3.45631| 
| 0.4| 1.4|2.82945|
| 0.5| 1.9|7.76261| 
| 0.6| 0.9| 2.7679| 
| 1.2| 1.0|9.87984| 
+----+----+-------+

# round 'col3' in a new column:
df2 = df.withColumn("col4", func.round(df["col3"], 2))
df2.show()

+----+----+-------+----+
|col1|col2|   col3|col4|
+----+----+-------+----+
| 0.0| 0.2|3.45631|3.46|
| 0.4| 1.4|2.82945|2.83|
| 0.5| 1.9|7.76261|7.76|
| 0.6| 0.9| 2.7679|2.77|
| 1.2| 1.0|9.87984|9.88|
+----+----+-------+----+

In the above data frame col4 is double. Now I want to convert col4 as Integer

df2 = df.withColumn("col4", func.round(df["col3"], 2).cast('integer'))

+----+----+-------+----+
|col1|col2|   col3|col4|
+----+----+-------+----+
| 0.0| 0.2|3.45631|   3|
| 0.4| 1.4|2.82945|   2|
| 0.5| 1.9|7.76261|   7|
| 0.6| 0.9| 2.7679|   2|
| 1.2| 1.0|9.87984|   9|
+----+----+-------+----+

But I want to round the col4 values to nearest

expected result

+----+----+-------+----+
|col1|col2|   col3|col4|
+----+----+-------+----+
| 0.0| 0.2|3.45631|   3|
| 0.4| 1.4|2.82945|   3|
| 0.5| 1.9|7.76261|   8|
| 0.6| 0.9| 2.7679|   3|
| 1.2| 1.0|9.87984|  10|
+----+----+-------+----+

How can I do that?


回答1:


You should use the round function and then cast to integer type. However, do not use a second argument to the round function. By using 2 there it will round to 2 decimal places, the cast to integer will then round down to the nearest number.

Instead use:

df2 = df.withColumn("col4", func.round(df["col3"]).cast('integer'))


来源:https://stackoverflow.com/questions/50636311/round-double-values-and-cast-as-integers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!