Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在数据帧中放置几个tweet_Python_Pandas_Dataframe_Twitter_Twitterapi Python - Fatal编程技术网

Python 在数据帧中放置几个tweet

Python 在数据帧中放置几个tweet,python,pandas,dataframe,twitter,twitterapi-python,Python,Pandas,Dataframe,Twitter,Twitterapi Python,我正试图下载巴拉克·奥巴马最后10条推文。然而,当我尝试将它们放入数据帧时,它只包含第10条tweet(因此只有1条)。有人知道如何解决这个问题吗?我先用打印而不是数据来尝试代码的顶部,然后我得到了所有10条推文,所以我不知道哪里出错了。我也没有收到错误消息 user = 'BarackObama' posts = tweepy.Cursor(api.user_timeline, screen_name=user,).items(10) for status in posts: if s

我正试图下载巴拉克·奥巴马最后10条推文。然而,当我尝试将它们放入数据帧时,它只包含第10条tweet(因此只有1条)。有人知道如何解决这个问题吗?我先用打印而不是数据来尝试代码的顶部,然后我得到了所有10条推文,所以我不知道哪里出错了。我也没有收到错误消息

user = 'BarackObama' 
posts = tweepy.Cursor(api.user_timeline, screen_name=user,).items(10)

for status in posts:
  if status.lang == 'en':
    data = {'User': [status.user.name],
        'Account name' ['@'+status.user.screen_name], 
        'Tweet': [status.text], 
        'Time': [status.created_at],
        'Nr of retweets': [status.retweet_count],
        'Nr of favorited': [status.favorite_count]}

df = pd.DataFrame(data)
   
df.head()

似乎您必须创建一个tweet的
列表
,然后将其放入
数据框

user = 'BarackObama' 
posts = tweepy.Cursor(api.user_timeline, screen_name=user,).items(10)
tweets = []

for status in posts:
  if status.lang == 'en':
    data = {'User': [status.user.name],
        'Account name' ['@'+status.user.screen_name], 
        'Tweet': [status.text], 
        'Time': [status.created_at],
        'Nr of retweets': [status.retweet_count],
        'Nr of favorited': [status.favorite_count]}
    tweets.append(data)

df = pd.DataFrame(tweets)
   
df.head()

谢谢成功了:)