如何将包含嵌套字典列表的列转换为数据框?

时间:2019-10-25 10:22:58

标签: python pandas dataframe

我有一列包含字典enter image description here的列 。字典具有三样东西,createdBy,CreatedAt和不同候选者的注释。我需要将它们放在单独的列中。我需要将此候选注释打开为字典内的三列。我尝试了以下操作,但这将创建6列enter image description here。我只需要3.嵌套的候选人注释列看起来像这样:enter image description here。候选者字典中有数据和时间,每个候选者ID使用时间和日期(在单独的列中),我需要在单独的列中更新状态,可能看起来像这样:enter image description here

df2 = pd.DataFrame.from_records(df1['candidateNotes']).add_prefix('s')
df2 = df2.s1.apply(pd.Series).add_prefix('') \
.merge(df2, left_index = True, right_index = True)
df2 = df2.s0.apply(pd.Series).add_prefix('') \
.merge(df2, left_index = True, right_index = True)

这是错误截图@qaiser:enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

import pandas as pd 

#creating dataframe which contain dictionary as row 
cf = pd.DataFrame([[[{'createdat':'abc','createdby':'asas','createdon':'lklj'}]],[[{'createdat':'aaaaa','createdby':'asas','createdon':'lklj'}]]]) 


df = pd.DataFrame()
for i in range(cf.shape[0]):
    df = df.append(pd.DataFrame([cf[0][i][0]]), ignore_index = True)

enter image description here

cf = pd.DataFrame([[[{ "createdAt": "2019-07-09T21:47:59.748Z", "notes": "Candidate initial submission.", "createdBy": "Akash D" }]],[[ { "createdAt": "2019-07-09T21:47:59.748Z", "note": "Candidate initial submission.", "createdBy": "Akash D","demo":"abc" } ]]], columns=['CandidateNotes']) 

print(cf)

enter image description here

 df = pd.DataFrame()
 for i in range(cf.shape[0]):
     try:
        df = df.append(pd.DataFrame([cf['CandidateNotes'][i][0]]), ignore_index = True)
     except:
        print(i)
 df

enter image description here