Python: Import. Reads unicode data in pandas data frame object

I am trying to import a file that has the structure below (dump tweets, with unicode lines). The goal is to convert this to a DataFrame using the pandas module. I assume the first step is to load the json object and then convert it to a DataFrame (on page 166 from McKinney Python for a data analysis book), but I'm not sure and can use some pointers to control this.

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... 
+4
source share
1 answer

Just use the DataFrame constructor ...

 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 

If you have the file as JSON, you can open it with json.load :

 import json with open('<MyFilePath>\usTweets0.json', 'r') as f: tweet_sample = json.load(f) 

Coming soon from_json to pandas ...

+2
source

Source: https://habr.com/ru/post/1484864/


All Articles