I have dataframe df
which has column called test_col
which contains json structures as shown below. As you can see lineItemPromotions object has nested jsons in it which can have 0-10 numbers of items in it. By unnesting, it should create new rows for each ID under lineItemPromotions.
How do I unnest this structures correctly?
{'provider': 'ABC',
'discountCodes_out': [],
'discounts_out': [],
'lineItemPromotions': [{'id': '1',
'discountCodes': [],
'discounts': [{'rule': 'Bundle Discount',
'name': 'Bundle Discount',
'ruleId': '',
'campaignId': '419f9a2f-0342-41c0-ac79-419d1023aaa9',
'centAmount': 1733550}],
'perUnitPromotionsShares': [1733550]},
{'id': '2',
'discountCodes': [],
'discounts': [{'rule': 'Bundle Discount',
'name': 'Bundle Discount',
'ruleId': '',
'campaignId': '419f9a2f-0342-41c0-ac79-419d1023aaa9',
'centAmount': 119438}],
'perUnitPromotionsShares': [119438, 119438]}]}
I tried following code but it is not working correctly. It is giving me nested item which I have to unnest again. Sorry that I have to paste the picture to show you the process how it is giving results.
2
Answers
You could first explode your columns then concat with a new df made from the
discounts
column:Output:
Although verbose, you could explicitly normalize each level:
You could then
.explode("perUnitPromotionsShares")
if desired.