Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Pandas Column which Consist of list of JSON into new columns

I have DataFrame which have 3 columns:

order_id  user_id  Details
5c7c9     A        [{"amount": "160",'id':'p2'},{"amount": "260",'id':'p3'}]
5c5c4     B        [{"amount": "10",'id':'p1'},{"amount": "260",'id':'p3'}]

I want my final Dataframe to be like:

order_id  user_id  amount  id
5c7c9     A        160     p2
5c7c9     A        260     p3
5c5c4     B        10      p1
5c5c4     B        260     p3
like image 404
sourav khanna Avatar asked Nov 30 '25 02:11

sourav khanna


2 Answers

First if necessary convert values to list of dictianaries by ast.literal_eval, then use dictionary comprehension with DataFrame constructor and concat and last use DataFrame.join for add to original:

import ast

#df['Details'] = df['Details'].apply(ast.literal_eval)

df1 = (pd.concat({k: pd.DataFrame(v) for k, v in df.pop('Details').items()})
         .reset_index(level=1, drop=True))

df = df.join(df1, rsuffix='_').reset_index(drop=True)
print (df)
  order_id user_id amount  id
0    5c7c9       A    160  p2
1    5c7c9       A    260  p3
2    5c5c4       B     10  p1
3    5c5c4       B    260  p3
like image 120
jezrael Avatar answered Dec 02 '25 17:12

jezrael


You can use:

s=pd.DataFrame([[x] + [z] for x, y in zip(df1.index,df1.Details) for z in y])
s=s.merge(df1,left_on=0,right_index=True).drop(['Details',0],1)
print(s.pop(1).apply(pd.Series).join(s))

  amount  id order_id user_id
0    160  p2    5c7c9       A
1    260  p3    5c7c9       A
2     10  p1    5c5c4       B
3    260  p3    5c5c4       B
like image 28
anky Avatar answered Dec 02 '25 17:12

anky