2017-02-15 67 views
0

我正在关注一个在线教程(http://adilmoujahid.com/posts/2014/07/twitter-analytics/),尽管将python脚本编写为相同,但我陷入了困境。我并不精通python,并且很难理解地图上的文档(本教程中使用这些文档)。现在我得到“valueError无法设置没有定义索引的框架和无法转换为Series的值”,并且无法找出修复程序。我的印象是数据框会有3列。一个包含所有推文,一个包含提到facebook的推文,另一个包含提到微软的所有推文。我也意识到,教程是两岁,所以也许有一些不赞成使用的语法?任何帮助表示赞赏Tweepy。在python熊猫数据框中存储tweet文本

import json 
import pandas as pd 
import re 

tweets_data_path = "Desktop/twit_dat/tweet1.txt" 
tweets_data = [] 

tweets_file = open(tweets_data_path, "r") 
for line in tweets_file: 
    try: 
     tweet = json.loads(line) 
     tweets_data.append(tweet) 
    except: 
     continue 


tweets = pd.DataFrame() 


tweets['text'] = map(lambda tweet: tweet['text'], tweets_data) 
tweets['Facebook'] = tweets['text'].apply(lambda tweet: word_in_text('Facebook', tweet)) 
tweets['Microsoft'] = tweets['text'].apply(lambda tweet: word_in_text('Microsoft', tweet)) 



def word_in_text(word,text): 
    if text == None: 
     return False 
    word = word.lower() 
    text = text.lower() 
    match = re.search(word,text) 
    if match: 
     return True 
    else: 
     return False 

这里是我使用的数据样本: http://charon.kean.edu/~jonathan/exampledata.txt

+0

我认为将文本文件直接导入数据框并不需要将数据附加到列表中会更容易。如果您发布原始文件的一些原始数据,我们可以尝试帮助您。 –

+0

@FabioLamanna谢谢,带有原始问题的样本数据的新链接 – overboard182

回答