我正在尝试导入具有以下结构的文件(使用unicode字符串转储推文)。目标是使用pandas模块将其转换为DataFrame。我假设第一步是加载到一个json对象然后转换为一个DataFrame(按照McKinney的Python for Data Analysis书中的第166页),但我不确定并且可以使用一些指针来管理它。
import sys, tailer
tweet_sample = tailer.head(open(r'<MyFilePath>\usTweets0.json'), 3)
tweet_sample # returns
['{u\'contributors\': None, u\'truncated\': False, u\'text\': u\'@KREAYSHAWN is...
答案 0 :(得分:2)
只需使用DataFrame构造函数......
In [6]: tweet_sample = [{'contributers': None, 'truncated': False, 'text': 'foo'}, {'contributers': None, 'truncated': True, 'text': 'bar'}]
In [7]: df = pd.DataFrame(tweet_sample)
In [8]: df
Out[8]:
contributers text truncated
0 None foo False
1 None bar True
如果您将文件作为JSON,则可以使用json.load
打开它:
import json
with open('<MyFilePath>\usTweets0.json', 'r') as f:
tweet_sample = json.load(f)
pandas将会有from_json
soon ......