Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert bytes data into a python pandas dataframe?

I would like to convert 'bytes' data into a Pandas dataframe.

The data looks like this (few first lines):

    (b'#Settlement Date,Settlement Period,CCGT,OIL,COAL,NUCLEAR,WIND,PS,NPSHYD,OCGT'  b',OTHER,INTFR,INTIRL,INTNED,INTEW,BIOMASS\n2017-01-01,1,7727,0,3815,7404,3'  b'923,0,944,0,2123,948,296,856,238,\n2017-01-01,2,8338,0,3815,7403,3658,16,'  b'909,0,2124,998,298,874,288,\n2017-01-01,3,7927,0,3801,7408,3925,0,864,0,2'  b'122,998,298,816,286,\n2017-01-01,4,6996,0,3803,7407,4393,0,863,0,2122,998' 

The columns headers appear at the top. each subsequent line is a timestamp and numbers.

Is there a straightforward way to do this?

Thank you very much

@Paula Livingstone:

This seems to work:

s=str(bytes_data,'utf-8')  file = open("data.txt","w")   file.write(s) df=pd.read_csv('data.txt') 

maybe this can be done without using a file in between.

like image 495
user7188934 Avatar asked Nov 19 '17 16:11

user7188934


People also ask

How do you convert a dataset to a DataFrame in Python?

You can convert the sklearn dataset to pandas dataframe by using the pd. Dataframe(data=iris. data) method.


1 Answers

I had the same issue and found this library https://docs.python.org/2/library/stringio.html from the answer here: How to create a Pandas DataFrame from a string

Try something like:

from io import StringIO  s=str(bytes_data,'utf-8')  data = StringIO(s)   df=pd.read_csv(data) 
like image 166
Tim Avatar answered Sep 18 '22 07:09

Tim