Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Byte/Dict to dataframe

General Tech Bugs & Fixes
Max. 2000 characters
Replies

usr_profile.png
Sarah Jones

User

( 7 months ago )

What is the best possible way to convert my data to DataFrame?

    data = b'{"word": "Gondwana", "date": "2019-03-27 13:07:12.404732"}'
           b'{"word": "alalus", "date": "2019-03-27 13:07:12.909517"}'
           b'{"word": "Balto-Slavonic", "date": "2019-03-27 13:07:14.911308"}'
           b'{"word": "peculatation", "date": "2019-03-27 13:07:15.421915"}'

I tried this. Didn't seem to work.

d = pd.DataFrame(dict(data))

usr_profile.png
Yasmin Mirza

User

( 7 months ago )

 

First decode values to utf-8 and convert to dictionaries in list comprehension by ast.literal_eval or json.loads:

data = [b'{"word": "Gondwana", "date": "2019-03-27 13:07:12.404732"}',
        b'{"word": "alalus", "date": "2019-03-27 13:07:12.909517"}',
        b'{"word": "Balto-Slavonic", "date": "2019-03-27 13:07:14.911308"}',
        b'{"word": "peculatation", "date": "2019-03-27 13:07:15.421915"}']

import ast   

df = pd.DataFrame([ast.literal_eval(x.decode("utf-8")) for x in data])
print (df)
                         date            word
0  2019-03-27 13:07:12.404732        Gondwana
1  2019-03-27 13:07:12.909517          alalus
2  2019-03-27 13:07:14.911308  Balto-Slavonic
3  2019-03-27 13:07:15.421915    peculatation

Alternative solution, should be faster in large data:

import json

df = pd.DataFrame([json.loads(x.decode("utf-8")) for x in data])

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif