Ulewsky
Ulewsky

Reputation: 329

Write bytes data into a data frame

I scraped one site and I get the data in bytes and I do not know how I can write them into a DF in some efficient way.

Here is a piece of my data:

b'[{"ID":"1","A":"2021-08-04 11:00:00","B":"2021-08-05 10:52:30","C":"","D":"2021-08-05 10:51:00","E":"","F":"Mark","G":"","H":"BOSTON"}]'

Upvotes: 0

Views: 132

Answers (1)

epattaro
epattaro

Reputation: 2428

Your problem is not how to transform bytes into a df, but how to transform bytes into a data structure that is "friendly" to df.

"eval" will try to transform the string/bytes into a data structure; in this case: a list of dictionaries

pd.DataFrame(eval(a), columns=eval(a)[0].keys())

note: if the bytes are valid jsons, its more efficient to use "json.loads" instead of "eval"

Upvotes: 1

Related Questions