【发布时间】:2014-06-20 07:25:06
【问题描述】:
我正在编写一个脚本,该脚本使用 Twitter API 从用户列表中提取最近的状态。我可以使用 API 检索数据,但是在将其转换为 DataFrame 后,我得到了存储字典的列。我想将这些字典的索引传播到其他列。最终,我试图将所有这些信息保存到 CSV。
代码如下:
import twython
import time
import pandas as pd
import numpy as np
app_key = ''
app_secret = ''
oauth_token = ''
oauth_token_secret = ''
twitter = twython.Twython(app_key, app_secret, oauth_token, oauth_token_secret)
screen_names = ['@', '@'] #enter screen names of interest
tweets = []
for screen_name in screen_names:
tweets.extend(twitter.get_user_timeline(screen_name=screen_name, count=200))
time.sleep(5)
df = pd.DataFrame(tweets)
返回一个 DataFrame (400,25)。 df[[2,3,5]] 返回以下内容:
created_at entities favorite_count
0 Thu Jun 19 13:14:39 +0000 2014 {u'symbols': [], u'user_mentions': [], u'hasht... 0
1 Thu Jun 19 11:53:51 +0000 2014 {u'symbols': [], u'user_mentions': [{u'id': 18... 0
2 Thu Jun 19 11:53:25 +0000 2014 {u'symbols': [], u'user_mentions': [], u'hasht... 3
3 Thu Jun 19 11:49:34 +0000 2014 {u'symbols': [], u'user_mentions': [], u'hasht... 0
4 Thu Jun 19 11:01:31 +0000 2014 {u'symbols': [], u'user_mentions': [{u'id': 18... 0
如何将 entities 列拆分为其他列?例如,我希望 symbols、user_mentions、hastags 等成为df。
非常感谢任何帮助。
【问题讨论】:
-
也许您可以通过查看此帖子找到解决方案:stackoverflow.com/a/23884434/1082349。在那里,有人描述了如何拆分位于一列中的 numpy 数组,以使每个数组值都位于一列中。
-
@FooBar 谢谢,正在调查。
df[entities].tolist()似乎把我带到了某个地方。
标签: python twitter pandas twython