仅对DoubleType的数据帧pyspark列进行舍入



我有以下功能:

def round_df(df_input: DataFrame) -> DataFrame:
for c in ([f.name for f in df_input.schema.fields if isinstance(f.dataType, DoubleType)]):
df_output = df_input.withColumn(c, round(sf.col(c), 3))
return df_output

我需要对df中DoubleType的列进行舍入,但当在我的df-pyspark中应用此函数时,它不起作用。有人能看到我的错误吗?

使用.dtypes很容易。

data_sdf. 
select(*[func.round(k, 2).alias(k) if typ == 'double' else k for k, typ in data_sdf.dtypes])

前面提到的只对双重类型的字段进行舍入,其余字段保持原样。

最新更新