在调用.show()
函数时,如何设置PySpark中的显示精度?
考虑以下示例:
from math import sqrt
import pyspark.sql.functions as f
data = zip(
map(lambda x: sqrt(x), range(100, 105)),
map(lambda x: sqrt(x), range(200, 205))
)
df = sqlCtx.createDataFrame(data, ["col1", "col2"])
df.select([f.avg(c).alias(c) for c in df.columns]).show()
输出结果为:
#+------------------+------------------+
#| col1| col2|
#+------------------+------------------+
#|10.099262230352151|14.212583322380274|
#+------------------+------------------+
我该如何更改它以便只显示小数点后三位数字?
期望的输出:
#+------+------+
#| col1| col2|
#+------+------+
#|10.099|14.213|
#+------+------+
这是一个PySpark版本的这个scala问题。我在这里发布它,因为当我搜索PySpark解决方案时找不到答案,我认为它对未来的其他人有帮助。