0

I have an output in the below bytes format. I would like to convert it to a Pandas Dataframe.

Actual Data

b'{"code":200,"data":{"facilities":[
  {"ref_id":"101","ref_name":"Product A","features":{"features1":"32","features2":"82"}},
  {"ref_id":"102","ref_name":"Product B","features":{"features1":"40","features2":"74"}},
  {"ref_id":"103","ref_name":"Product C","features":{"features1":"33","features2":"84"}},
  {"ref_id":"104","ref_name":"Product D","features":{"features1":"36","features2":"115"}},
  {"ref_id":"105","ref_name":"Product E","features":{"features1":"32","features2":"95"}},
  {"ref_id":"106","ref_name":"Product F","features":{"features1":"35","features2":"89"}},
  {"ref_id":"107","ref_name":"Product G","features":{"features1":"38","features2":"90"}}]},"request_id":"090"}'

Expected output (Dataframe):

ref_id, ref_name, features1, features2
101, Product A, 32, 82
102, Product B, 40, 74
103, Product C, 33, 84
104, Product D, 36, 115
105, Product E, 32, 95
106, Product F, 35, 89
107, Product G, 38, 90

1 Answer 1

1

Try json and pd.json_normalize:

import json

# parse json to a dictionary
data = json.loads(s)

df = pd.json_normalize(data, record_path=['data','facilities'])

Output:

  ref_id   ref_name features.features1 features.features2
0    101  Product A                 32                 82
1    102  Product B                 40                 74
2    103  Product C                 33                 84
3    104  Product D                 36                115
4    105  Product E                 32                 95
5    106  Product F                 35                 89
6    107  Product G                 38                 90
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.