简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM