【问题标题】:How to create data frames from rdd of word's list如何从单词列表的rdd创建数据框
【发布时间】:2016-11-08 07:27:17
【问题描述】:

我已经浏览了 stackoverflow 和互联网上的所有答案,但没有任何效果。所以我有这个单词列表的 rdd:

tweet_words=['tweet_text',
 'RT',
 '@ochocinco:',
 'I',
 'beat',
 'them',
 'all',
 'for',
 '10',
 'straight',
 'hours']


**What i have done till now:**

Df =sqlContext.createDataFrame(tweet_words,["tweet_text"])

and 

tweet_words.toDF(['tweet_words'])

**ERROR**:

TypeError: Can not infer schema for type: <class 'str'>

【问题讨论】:

    标签: pyspark


    【解决方案1】:

    查看上面的代码,您正在尝试将列表转换为 DataFrame。一个很好的 StackOverflow 链接是:https://stackoverflow.com/a/35009289/1100699

    这么说,这是您的代码的工作版本:

    from pyspark.sql import Row
    
    # Create RDD
    tweet_wordsList = ['tweet_text', 'RT', '@ochocinco:', 'I', 'beat', 'them', 'all', 'for', '10', 'straight', 'hours']
    tweet_wordsRDD = sc.parallelize(tweet_wordsList)
    
    # Load each word and create row object
    wordRDD = tweet_wordsRDD.map(lambda l: l.split(","))
    tweetsRDD = wordRDD.map(lambda t: Row(tweets=t[0]))
    
    # Infer schema (using reflection)
    tweetsDF = tweetsRDD.toDF()
    
    # show data
    tweetsDF.show()
    

    HTH!

    【讨论】:

    • tweet_words 列表中有很多数据,我可以在 sc.parallelize() 方法中使用变量 tweet_words。
    • 是的,我已经更新了上面的示例以访问变量tweet_wordsList。 HTH!
    • 嗨 Denny,TypeError: 'PipelinedRDD' object is not iterable 正在发生
    • 这只是发生在 sc.parallelize(list) 上吗?还是下线?
    猜你喜欢
    • 1970-01-01
    • 2021-12-10
    • 2019-07-14
    • 2017-07-02
    • 2022-07-22
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多