'How to get the Cartesian product of 2 columns by row

I have some difficulities that if there's a spark dataframe and there is a column called "id" and another column called "friends"

+---+--------------------+
| id|             friends|
+---+--------------------+
|  0|[1, 2, 3, 4, 5, 6...|
|  1|[48, 53, 54, 73, ...|
|  2|[20, 115, 116, 14...|
|  3|[9, 25, 26, 67, 7...|
|  4|[78, 152, 181, 19...|
|  5|[87, 122, 156, 15...|
|  6|[89, 95, 147, 219...|
|  7|[22, 31, 38, 65, ...|
|  8|[91, 110, 193, 20...|
|  9|[21, 25, 26, 30, ...|
| 10|[67, 142, 169, 20...|
| 13|[21, 26, 56, 59, ...|
| 14|[20, 28, 41, 115,...|
| 16|[29, 82, 118, 172...|
| 17|[19, 41, 111, 115...|
| 19|[41, 89, 112, 115...|
| 20|[41, 44, 111, 115...|
| 21|[25, 26, 31, 39, ...|
| 22|[87, 158, 168, 18...|
| 23|[51, 61, 83, 99, ...|
+---+--------------------+

How can I get the cartesian product for each row? For instance, the first row we should get (0,1) (0,2) (0,3)..etc second row should get (1,48) (1,53)..



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source