I would like to compute the maximum of a subset of columns for each row and add it as a new column for the existing Dataframe
.
I managed to do this in very awkward way:
def add_colmax(df,subset_columns,colnm):
'''
calculate the maximum of the selected "subset_columns" from dataframe df for each row,
new column containing row wise maximum is added to dataframe df.
df: dataframe. It must contain subset_columns as subset of columns
colnm: Name of the new column containing row-wise maximum of subset_columns
subset_columns: the subset of columns from w
'''
from pyspark.sql.functions import monotonicallyIncreasingId
from pyspark.sql import Row
def get_max_row_with_None(row):
return float(np.max(row))
df_subset = df.select(subset_columns)
rdd = df_subset.map( get_max_row_with_None)
df_rowsum = rdd.map(Row(colnm)).toDF()
df_rowsum = df_rowsum.withColumn("id",monotonicallyIncreasingId())
df = df.withColumn("id",monotonicallyIncreasingId())
df = df.join(df_rowsum,df.id == df_rowsum.id).drop(df.id).drop(df_rowsum.id)
return df
This function works as:
rdd1 = sc.parallelize([("foo", 1.0,3.0,None),
("bar", 2.0,2.0,-10),
("baz", 3.3,1.2,10.0)])
df1 = sqlContext.createDataFrame(rdd1, ('v1', 'v2','v3','v4'))
df_new = add_colmax(df1,['v2','v3','v4'],"rowsum")
df_new.collect()
returns:
[Row(v1=u'bar', v2=2.0, v3=2.0, v4=-10, rowsum=2.0),
Row(v1=u'baz', v2=3.3, v3=1.2, v4=None, rowsum=3.3),
Row(v1=u'foo', v2=1.0, v3=3.0, v4=None, rowsum=3.0)]
I think that if I could use user defined functions with withColumn
, this can be done much simpler. But I could not figure out how to do it.
Please let me know if you have simpler way to achieve this.
I am using Spark 1.6
Let's start with a couple of imports
from pyspark.sql.functions import col, lit, coalesce, greatest
Next define minus infinity literal:
minf = lit(float("-inf"))
Map columns and pass the result to greatest
:
rowmax = greatest(*[coalesce(col(x), minf) for x in ['v2','v3','v4']])
Finally withColumn
:
df1.withColumn("rowmax", rowmax)
with result:
+---+---+---+----+------+
| v1| v2| v3| v4|rowmax|
+---+---+---+----+------+
|foo|1.0|3.0|null| 3.0|
|bar|2.0|2.0| -10| 2.0|
|baz|3.3|1.2|null| 3.3|
+---+---+---+----+------+
You can use the same pattern with different row wise operations replacing minf
with neutral element. For example:
rowsum = sum([coalesce(col(x), lit(0)) for x in ['v2','v3','v4']])
or:
from operator import mul
from functools import reduce
rowproduct = reduce(
mul,
[coalesce(col(x), lit(1)) for x in ['v2','v3','v4']]
)
Your own code could be significantly simplified with udf
:
from pyspark.sql.types import DoubleType
from pyspark.sql.functions import udf
def get_max_row_with_None_(*cols):
return float(max(x for x in cols if x is not None))
get_max_row_with_None = udf(get_max_row_with_None_, DoubleType())
df1.withColumn("rowmax", get_max_row_with_None('v2','v3','v4'))
Replace minf
with lit(float("inf"))
and greatest
with least
to get the smallest value per row.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With