Pandas: Explode list of dictionary of a specific row into multiple rows

While I can’t make any claims about efficiency, perhaps this does what you need.

op_d_new = {k:(v[0] if v[0][0]=='NULL' else \
               [list(j.values())[0] for j in v[0]]) for k,v in op_d.items()}
for v in op_d.values():
    if v[0][0] == 'NULL':
        continue
    dates = [list(j.keys())[0] for j in v[0]]
    break
op_d_new.update({'Dates':dates})

df = pd.DataFrame(op_d_new)
df
      A           V           Q     Z Dates
0  NULL  488.386000  488.386000  NULL  2016
1  NULL  469.508000  469.508000  NULL  2017
2  NULL  445.510000  445.510000  NULL  2018
3  NULL  579.299000  579.299000  NULL  2019
4  NULL  493.786000  493.786000  NULL  2020

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top