【问题标题】:"normalize" dataframe of sentences into larger dataframe of words将句子的数据框“规范化”为更大的单词数据框
【发布时间】:2015-12-16 15:10:21
【问题描述】:

使用 Python 和 Spark:

假设我有一个包含句子的行的 DataFrame,我如何 normalize(从 DBMS 术语)将句子 DataFrame 转换为另一个 DataFrame,每行包含一个从句子中拆分出来的单词?

我认为这主要是telegraph problem

例如,假设df_sentences 看起来像这样:

[Row(sentence_id=1, sentence=u'the dog ran the fastest.'),
 Row(sentence_id=2, sentence=u'the cat sat down.')]

我正在寻找将df_sentences 转换为df_words 的转换,它将采用这两行并构建一个更大的(行数)DataFrame,如下所示。注意 sentence_id 被带到了新表中:

[Row(sentence_id=1, word=u'the'),
 Row(sentence_id=1, word=u'the'),
 Row(sentence_id=1, word=u'fastest'), 
 Row(sentence_id=2, word=u'dog'),
 Row(sentence_id=2, word=u'ran'), 
 Row(sentence_id=2, word=u'cat'), 
 ...clip...]

现在,目前我对行数或唯一词并不真正感兴趣,那是因为我想加入 sentence_id 上的其他 RDD 以获取我在其他地方存储的其他有趣数据。

我怀疑 Spark 中的大部分能力都在于管道中的这些间歇性转换,因此我想了解做事的最佳方式并开始收集我自己的 sn-ps/etc。

【问题讨论】:

标签: python apache-spark dataframe pyspark apache-spark-sql


【解决方案1】:

其实很简单。让我们从创建一个DataFrame 开始:

from pyspark.sql import Row

df = sc.parallelize([
    Row(sentence_id=1, sentence=u'the dog ran the fastest.'),
     Row(sentence_id=2, sentence=u'the cat sat down.')
]).toDF()

接下来我们需要一个分词器:

from pyspark.ml.feature import RegexTokenizer

tokenizer = RegexTokenizer(
    inputCol="sentence", outputCol="words", pattern="\\W+")
tokenized = tokenizer.transform(df)

最后我们放弃sentenceexplode words:

from pyspark.sql.functions import explode, col

transformed = (tokenized
    .drop("sentence")
    .select(col("sentence_id"), explode(col("words")).alias("word")))

终于出结果了:

transformed.show()

## +-----------+-------+
## |sentence_id|   word|
## +-----------+-------+
## |          1|    the|
## |          1|    dog|
## |          1|    ran|
## |          1|    the|
## |          1|fastest|
## |          2|    the|
## |          2|    cat|
## |          2|    sat|
## |          2|   down|
## +-----------+-------+

注意事项

  • 依赖于数据explode 可能会相当昂贵,因为它会复制其他列。请务必在应用 explode 之前应用所有过滤器,例如 StopWordsRemover

【讨论】:

    猜你喜欢
    • 2021-04-05
    • 2020-08-01
    • 2021-11-13
    • 2021-12-15
    • 2020-07-09
    • 2017-05-04
    • 2020-07-22
    • 2014-12-12
    • 1970-01-01
    相关资源
    最近更新 更多