I have the following spark dataframe:
import pandas as pd
foo = pd.DataFrame({'id': [1,2,3,4], 'col': [[1,2,None, None], [1,1,2,2], [1,1,1,2], [1,None,None,None]]})
foo_dfs = spark.createDataFrame(foo)
foo_dfs.show()
+---+------------+
| id| col|
+---+------------+
| 1| [1, 2,,]|
| 2|[1, 1, 2, 2]|
| 3|[1, 1, 1, 2]|
| 4| [1,,,]|
+---+------------+
I am creating the min_col and max_col by doing the following:
from pyspark.sql import functions as f
foo_dfs = foo_dfs.withColumn('min_col', f.array_min('col'))
foo_dfs = foo_dfs.withColumn('max_col', f.array_max('col'))
which outputs:
+---+------------+-------+-------+
| id| col|min_col|max_col|
+---+------------+-------+-------+
| 1| [1, 2,,]| 1| 2|
| 2|[1, 1, 2, 2]| 1| 2|
| 3|[1, 1, 1, 2]| 1| 2|
| 4| [1,,,]| 1| 1|
+---+------------+-------+-------+
I would like to also add the mean_col and median_col next to the max_col. How can I do that ? As far as I know there is no f.array_mean or f.array_median function
The output dataframe should look like:
+---+------------+-------+-------+--------+----------+
| id| col|min_col|max_col|mean_col|median_col|
+---+------------+-------+-------+--------+----------+
| 1| [1, 2,,]| 1| 2| 1.5| 1.5|
| 2|[1, 1, 2, 2]| 1| 2| 1.5| 1.5|
| 3|[1, 1, 1, 2]| 1| 2| 1.25| 1.0|
| 4| [1,,,]| 1| 1| 1.0| 1.0|
+---+------------+-------+-------+--------+----------+
So the mean_col and median_col calculation should ignore the None values
I tried this:
import numpy as np
array_median = f.udf(lambda x: float(np.nanmedian(x)), FloatType())
array_mean = f.udf(lambda x: float(np.nanmean(x)), FloatType())
foo_dfs.withColumn('median_col', array_median('col'))
foo_dfs.withColumn('mean_col', array_mean('col'))
But it didn't work, I am getting the following error:
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
Any ideas?