我正在尝试提取旧的推文,然后将各种属性放入Pandas数据帧。我的代码如下。但是,当我运行最后一行时,我得到的数据帧只包含一条推文的属性数据,而不是我选择的所有数据。
有人能告诉我我的错误在哪里吗?如何将每条推文作为单独的行添加到数据框中?
import got3
#Need this to get older tweets
tweetCriteria = got3.manager.TweetCriteria()
tweetCriteria.setQuerySearch("Kentucky Derby")
tweetCriteria.setSince("2016-05-07")
tweetCriteria.setUntil("2016-05-08")
tweetCriteria.setMaxTweets(1000)
#Here I set the parameters for the old tweets I am seeking
TweetCriteria = got3.manager.TweetCriteria()
KYDerby_tweets = got3.manager.TweetManager.getTweets(tweetCriteria)
#Named my list of tweets
for x in KYDerby_tweets:
Text = x.text
Retweets = x.retweets
Favorites = x.favorites
Date = x.date
Id = x.id
#created variables for each of the attributes I want in the dataframe
df = pd.DataFrame(DataSet)
df
#result here is a 1x6 dataframe containing just one tweet, rather than the 1000x6 dataframe I want, which would contain all of the tweets from my selection...
答案 0 :(得分:0)
在您的评论澄清后,这应该有效:
# make a list of lists that contain the data
# pandas considers a list of lists to be a list of rows
data_set = [[x.id, x.date, x.text, x.retweets, x.favorites, x.date, x.id]
for x in KYDerby_tweets]
df = pd.DataFrame(data=data_set, columns=["Id", "Date", "Text", "Favorites", "Retweets"])