I have a category list that already been flattened but not sure how I can convert it to dataframe like below example. So if a parent category id is equal to 0 then that will be my id and then if has children is true I'll get the name and so on.
"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 |
|---|---|---|---|---|
| 100017 | Women Clothes | Socks & Stockings | Others | |
| 100017 | Women Clothes | Socks & Stockings | Pantyhose | |
| 100017 | 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())
