Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用自定义udf实现对列进行四舍五入_Python_Pyspark_User Defined Functions_Floor_Ceil - Fatal编程技术网

Python 如何使用自定义udf实现对列进行四舍五入

Python 如何使用自定义udf实现对列进行四舍五入,python,pyspark,user-defined-functions,floor,ceil,Python,Pyspark,User Defined Functions,Floor,Ceil,我有一个pyspark数据帧,如: +-------------------+ | to_return_day| +-------------------+ | -2.003125| | -20.96738425925926| | -2.332546296296296| | -2.206770833333333| |-2.9733564814814817| | 54.71157407407407| | 51.70229166666667| |48.6663541666

我有一个pyspark数据帧,如:

+-------------------+
|      to_return_day|
+-------------------+
|          -2.003125|
| -20.96738425925926|
| -2.332546296296296|
| -2.206770833333333|
|-2.9733564814814817|
| 54.71157407407407|
| 51.70229166666667|
|48.666354166666665|
| 9.665497685185185|
| 49.56260416666667|
| 66.68983796296297|
| 49.80550925925926|
|  66.6899074074074|

我想用一个udf来实现“to_return_day”>0时的四舍五入,以及“to_return_day”时的向下四舍五入。我可能还没有完全理解Q OP发布的内容。据我所知,OP想要的输出是-

1) 对于正值(大于等于0),取该数值上方最接近的整数值,例如:;对于2.34,它将是3

2) 对于负值,为该数值以下的最近整数值,例如;对于-2.34,它将是-3

# Creating the DataFrame
values = [(-2.003125,),(-20.96738425925926,),(-2.332546296296296,),(-2.206770833333333,),
          (-2.9733564814814817,),(54.71157407407407,),(51.70229166666667,),(48.666354166666665,),
          (9.665497685185185,),(49.56260416666667,),(66.68983796296297,),(49.80550925925926,),
          (66.6899074074074,),]
df = sqlContext.createDataFrame(values,['to_return_day',])
df.show()
+-------------------+
|      to_return_day|
+-------------------+
|          -2.003125|
| -20.96738425925926|
| -2.332546296296296|
| -2.206770833333333|
|-2.9733564814814817|
|  54.71157407407407|
|  51.70229166666667|
| 48.666354166666665|
|  9.665497685185185|
|  49.56260416666667|
|  66.68983796296297|
|  49.80550925925926|
|   66.6899074074074|
+-------------------+
当使用简单的
语句时,如果else
语句足够,则无需创建
UDF

# Importing relevant functions
from pyspark.sql.functions import ceil, floor, when
df = df.withColumn('to_return_day',when(col('to_return_day') >=0 , ceil(col('to_return_day'))).otherwise(floor(col('to_return_day'))))
df.show()
+-------------+
|to_return_day|
+-------------+
|           -3|
|          -21|
|           -3|
|           -3|
|           -3|
|           55|
|           52|
|           49|
|           10|
|           50|
|           67|
|           50|
|           67|
+-------------+
文件:和

如果您只希望使用
UDF
,那么下面的代码将起作用

# Import relevant functions and packages.
from pyspark.sql.functions import udf, col
import math
# Defining a UDF
def round_udf(c):
    if c < 0:
        return math.floor(c)
    else:
        return math.ceil(c)

round_udf = udf(round_udf,IntegerType())

df = df.withColumn('to_return_day',round_udf(col('to_return_day')))
#导入相关函数和包。
从pyspark.sql.functions导入udf,col
输入数学
#定义自定义项
def圆形_udf(c):
如果c<0:
返回数学楼层(c)
其他:
return math.ceil(c)
round\u udf=udf(round\u udf,IntegerType())
df=df.withColumn('to\u return\u day',round\u udf(col('to\u return\u day'))

我可能没有完全理解Q OP发布的内容。据我所知,OP想要的输出是-

1) 对于正值(大于等于0),取该数值上方最接近的整数值,例如:;对于2.34,它将是3

2) 对于负值,为该数值以下的最近整数值,例如;对于-2.34,它将是-3

# Creating the DataFrame
values = [(-2.003125,),(-20.96738425925926,),(-2.332546296296296,),(-2.206770833333333,),
          (-2.9733564814814817,),(54.71157407407407,),(51.70229166666667,),(48.666354166666665,),
          (9.665497685185185,),(49.56260416666667,),(66.68983796296297,),(49.80550925925926,),
          (66.6899074074074,),]
df = sqlContext.createDataFrame(values,['to_return_day',])
df.show()
+-------------------+
|      to_return_day|
+-------------------+
|          -2.003125|
| -20.96738425925926|
| -2.332546296296296|
| -2.206770833333333|
|-2.9733564814814817|
|  54.71157407407407|
|  51.70229166666667|
| 48.666354166666665|
|  9.665497685185185|
|  49.56260416666667|
|  66.68983796296297|
|  49.80550925925926|
|   66.6899074074074|
+-------------------+
当使用简单的
语句时,如果else
语句足够,则无需创建
UDF

# Importing relevant functions
from pyspark.sql.functions import ceil, floor, when
df = df.withColumn('to_return_day',when(col('to_return_day') >=0 , ceil(col('to_return_day'))).otherwise(floor(col('to_return_day'))))
df.show()
+-------------+
|to_return_day|
+-------------+
|           -3|
|          -21|
|           -3|
|           -3|
|           -3|
|           55|
|           52|
|           49|
|           10|
|           50|
|           67|
|           50|
|           67|
+-------------+
文件:和

如果您只希望使用
UDF
,那么下面的代码将起作用

# Import relevant functions and packages.
from pyspark.sql.functions import udf, col
import math
# Defining a UDF
def round_udf(c):
    if c < 0:
        return math.floor(c)
    else:
        return math.ceil(c)

round_udf = udf(round_udf,IntegerType())

df = df.withColumn('to_return_day',round_udf(col('to_return_day')))
#导入相关函数和包。
从pyspark.sql.functions导入udf,col
输入数学
#定义自定义项
def圆形_udf(c):
如果c<0:
返回数学楼层(c)
其他:
return math.ceil(c)
round\u udf=udf(round\u udf,IntegerType())
df=df.withColumn('to\u return\u day',round\u udf(col('to\u return\u day'))