I have a category list that already been flattened but not sure how I can convert it to table that looks like below example. So if has children is equal to false then that will be my id and then if has children is true I'll get the name and so on. I think it can be done by looping the category list
"response": {
"category_list": [
{
"category_id": 100017,
"parent_category_id": 0,
"name": "Women Clothes",
"has_children": true
},
{
"category_id": 100118,
"parent_category_id": 100017,
"name": "Socks & Stockings",
"has_children": true
},
{
"category_id": 100419,
"parent_category_id": 100118,
"name": "Others",
"has_children": false
},
{
"category_id": 100418,
"parent_category_id": 100118,
"name": "Pantyhose",
"has_children": false
},
{
"category_id": 100417,
"parent_category_id": 100118,
"name": "Socks",
"has_children": false
}
]
}
So result should be like this:
| Id | Category 1 | Category 2 | Category 3 | Category 4 |
|---|---|---|---|---|
| 100419 | Women Clothes | Socks & Stockings | Others | |
| 100417 | Women Clothes | Socks & Stockings | Pantyhose | |
| 100418 | Women Clothes | Socks & Stockings | Socks |
CodePudding user response:
You are looking for -
import pandas as pd
pd.DataFrame.from_records(d['category_list'])
Assuming d is a dictionary, and d['category_list'] is a list
CodePudding user response:
The easiest way to do this is by using python pandas to create a data frame from JSON
import pandas as pd
df = pd.read_json('data.json')
print(df.to_string())
