How to split a nested list inside a Pandas Dataframe column vertically,ie, into separate rows

Question:

I have a Dataframe, I want to split Co2 rowise :

Co1 Co2
1 [{'link':'http:abc.com'},{'link':'http:def.com'}]
2 [{'link':'http:ghi.com'},{'link':'http:lmn.com'}]

Expected output

Co1 Co2
1 http:abc.com
1 http:def.com
2 http:ghi.com
2 http:lmn.com
Asked By: Shabari nath k

||

Answers:

Use concat with dicionary of DataFrame created in list comprehension and join by DataFrame.join to original, DataFrame.pop is used for extract column:

import ast

#if values are string repr of lists
dfs = {k:pd.DataFrame(ast.literal_eval(x)) for k, x in df.pop('Co2').items()}
#if values are lists of dicts
#dfs = {k:pd.DataFrame(x) for k, x in df.pop('Co2').items()}

df = df.join(pd.concat(dfs).reset_index(level=1, drop=True)).reset_index(drop=True)
print (df)
   Co1          link
0    1  http:abc.com
1    1  http:def.com
2    2  http:ghi.com
3    2  http:lmn.com
Answered By: jezrael

Another way with explode and series.str.get:

#df['Co2'] = df['Co2'].apply(ast.literal_eval) : if Co2 are not actual lists
out = df.explode('Co2').assign(Co2 = lambda x: 
                              x['Co2'].str.get('link')).reset_index(drop=True)

print(out) 

   Co1           Co2
0    1  http:abc.com
1    1  http:def.com
2    2  http:ghi.com
3    2  http:lmn.com
Answered By: anky
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.