I have a table, where I would like to loop through the array in column "Type". If it contains 'cover', take the value from the "ID" array of the same position. And if there are 2 'cover' values, check the "style" column - if it is 'multi', ignore and take other value position.
| ID | Type | style |
|---|---|---|
| [222, 345, 678] | ['leg','cover','cover'] | ['modern','multi','traditional'] |
| [989, 787, 125] | ['cover','hanger','comp'] | ['modern','modern','modern'] |
The desired output:
| ID | Type | style | output |
|---|---|---|---|
| [222, 345, 678] | ['leg','cover','cover'] | ['modern','multi','traditional'] | 678 |
| [989, 787, 125] | ['cover','hanger','comp'] | ['modern','modern','modern'] | 989 |
I am using this code to get the position but I am stuck with the rest.
df.select(df.Type, array_position(df.Type, "cover").alias('a_pos')).show()