Let's say I have a dataframe like this:
+-----------+-----------+-----------+-----------+------------+--+
| ColA | ColB | ColC | ColD | ColE | |
+-----------+-----------+-----------+-----------+------------+--+
| '' | sample_1x | sample_1y | '' | sample_1z | |
| sample2_x | sample2_y | '' | '' | '' | |
| sample3_x | '' | '' | '' | sample3_y | |
| sample4_x | sample4_y | '' | sample4_z | sample4_zz | |
| sample5_x | '' | '' | '' | '' | |
+-----------+-----------+-----------+-----------+------------+--+
I want to create another dataframe that shows the relationship from left to right in each row, while skipping columns that have empty values. Also rows that only have 1 valid columnar record will be excluded. For example:
+-----------+------------+-----------+
| From | To | Label |
+-----------+------------+-----------+
| sample1_x | sample1_y | ColB_ColC |
| sample1_y | sample1_z | ColC_ColE |
| sample2_x | sample2_y | ColA_ColB |
| sample3_x | sample3_y | ColA_ColE |
| sample4_x | sample4_y | ColA_ColB |
| sample4_y | sample4_z | ColB_ColD |
| sample4_z | sample4_zz | ColD_ColE |
+-----------+------------+-----------+
I'm thinking the approach would be to write a UDF that contains this logic but I'm not entirely sure how I would return a completely new DF, as I'm used to UDFs just creating another column within the same DF. Or if there's another spark function that can handle this case easier than creating a UDF? Using pyspark if that matters.