我想根据行的每个值计算逐行平均值和次平均值,并在结束时获得最大值
here is my dataframe
col1 | col2 | col3
0 | 2 | 3
4 | 2 | 3
1 | 0 | 3
0 | 0 | 0
df=df.withColumn("mean_value",(sum(col(x) for x in df.columns[0:2]) / 3).alias("mean"))
我可以用代码行计算逐行平均值,但我想从行的每个值中减去平均值,并在减去平均值后得到行的最大值。
Requeire results
col1 | col2 | col3 mean_Value Max_difference_Value
0 | 2 | 3 1.66 1.34
4 | 2 | 3 3.0 1.0
1 | 0 | 3 1.33 1.67
1 | 0 | 1 0.66 0.66
Note this is main formula: abs(mean-columns value).max()
使用greatest
和列表理解。
spark.sparkContext.parallelize(data_ls).toDF(['col1', 'col2', 'col3']).
withColumn('mean_value', (sum(func.col(x) for x in ['col1', 'col2', 'col3']) / 3)).
withColumn('max_diff_val',
func.greatest(*[func.abs(func.col(x) - func.col('mean_value')) for x in ['col1', 'col2', 'col3']])
).
show()
# +----+----+----+------------------+------------------+
# |col1|col2|col3| mean_value| max_diff_val|
# +----+----+----+------------------+------------------+
# | 0| 2| 3|1.6666666666666667|1.6666666666666667|
# | 4| 2| 3| 3.0| 1.0|
# | 1| 0| 3|1.3333333333333333|1.6666666666666667|
# | 0| 0| 0| 0.0| 0.0|
# +----+----+----+------------------+------------------+
您尝试过UDF吗?
from pyspark.sql.types import FloatType
from pyspark.sql.functions import udf
import numpy as np
@udf
def udf_mean(col1, col2, col3):
return np.mean([col1, col2, col3])
df = df.withColumn("mean_value", udf_mean(col1, col2, col3))
同样,您可以尝试最大差值。