I have the following dataframe
df_old_list= [
{ "Col1":"0", "Col2" : "7","Col3": "8", "Col4" : "","Col5": "20"},
{"Col1":"5", "Col2" : "5","Col3": "5", "Col4" : "","Col5": "28"},
{ "Col1":"-1", "Col2" : "-1","Col3": "13", "Col4" : "","Col5": "83"},
{"Col1":"-1", "Col2" : "6","Col3": "6", "Col4" : "","Col5": "18"},
{ "Col1":"5", "Col2" : "4","Col3": "2", "Col4" : "","Col5": "84"},
{ "Col1":"0", "Col2" : "0","Col3": "14", "Col4" : "7","Col5": "86"}
]
spark = SparkSession.builder.getOrCreate()
df_old_list = spark.createDataFrame(Row(**x) for x in df_old_list)
df_old_list.show()
+----+----+----+----+----+
|Col1|Col2|Col3|Col4|Col5|
+----+----+----+----+----+
| 0| 7| 8| | 20|
| 5| 5| 5| | 28|
| -1| -1| 13| | 83|
| -1| 6| 6| | 18|
| 5| 4| 2| | 84|
| 0| 0| 14| 7| 86|
+----+----+----+----+----+
I want to get the lowest value from each column for each row.
This is what i was able to achieve so far
df1=df_old_list.selectExpr("*","array_sort(split(concat_ws(',',*),','))[0] lowest_col")
df1.show()
+----+----+----+----+----+----------+
|Col1|Col2|Col3|Col4|Col5|lowest_col|
+----+----+----+----+----+----------+
| 0| 7| 8| | 20| |
| 5| 5| 5| | 28| |
| -1| -1| 13| | 83| |
| -1| 6| 6| | 18| |
| 5| 4| 2| | 84| |
| 0| 0| 14| 7| 86| 0|
+----+----+----+----+----+----------+
The problem is Col4 is blank and therefore its not able to compute the lowest value. What i am looking for is something like this : where i get the lowest value irrespective of blank columns and also if there are more than one lowest numbers then that column fields names should be shown in the lowest_col_title as concatenated.
+-----------------+----------+----+----+----+----+----+
|lowest_cols_title|lowest_col|Col1|Col2|Col3|Col4|Col5|
+-----------------+----------+----+----+----+----+----+
| Col1| 0| 0| 7| 8| | 20|
| Col1;Col2;Col3| 5| 5| 5| 5| | 28|
| Col1;Col2| -1| -1| -1| 13| | 83|
| Col1| -1| -1| 6| 6| | 18|
| Col3| 5| 5| 4| 2| | 84|
| Col1;Col2| 0| 0| 0| 14| 7| 86|
+-----------------+----------+----+----+----+----+----+
