在 pandas 数据框中有一个嵌套的嵌套列,这是我在 json_normalize 请求后收到的,如下所示:
{
'Speed':352,
'type':[
{
'details':'change\n',
'id':'3129f48c-23a6-49bd-9854',
'machine':'logs',
'Cause':{
'code':'051',
'description':'Error',
'id':'41827d41-75c5-4c88-9ec3',
'reason':'Error'
}
}
],
'endTime':1522844263021,
'line':'73c2f337-46fc-415c-a24f',
'level':1,
'lineId':'9b6a5624-4add-4fce-9de3',
'loss':0,
'startTime':1522842642200
}
如何将其拆分为列?我尝试使用 lambda 方法,但它给了我错误 'list index out of range'
而且我不知道如何处理第二个嵌套:
df['details'] = df.loc[:, 'type'].apply(lambda x: x[0]['details'])
如何解决这个任务?
最佳答案
json_normalize () 的选项帮助我解决了这种情况:
df_df = json_normalize(df['values'], record_path=['type'], meta=['Speed','endTime', 'level','lineId', 'loss', 'startTime'])
关于python - Pandas Dataframe 中的嵌套嵌套列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50467244/