计算数组数据框中的出现次数

Count occurrences in dataframe of arrays

我有一个如下所示的数据框:

|--------------------------|
|          text            |     
|--------------------------|
|  ["hello", "world"]      |
|--------------------------|
|  ["foo"]                 |
|--------------------------|
|  ["world", "bar"]        |
|--------------------------|
|  ["kung", "foo", "world"]|
|--------------------------|
|          ...             |

我需要计算每个单词出现的次数,并从大到小排序。我不知道它可能包含的所有单词。我该如何操纵它来做到这一点?

完成后看起来像这样

|-----------------|-----------------|
|        word     |       count     |
|-----------------|-----------------|
|     hello       |         1       |
|-----------------|-----------------|
|     world       |         3       |
|-----------------|-----------------|
|     bar         |         1       |
|-----------------|-----------------|
|     foo         |         2       |
|-----------------|-----------------|
|     ...         |        ...      |

我试过展平数据帧、在其上映射等,但我超级卡住了。如有任何帮助,我们将不胜感激!

假设已存储为字符串列表:

df.select(explode($"text").as("word")).groupBy("word").count.orderBy(desc("count"))