将具有相同键的字典列表转换为高数据框

时间:2019-01-21 03:01:19

标签: python pandas

数据框具有一列,其中包含具有相同键名的字典列表。如何将其转换为高数据框?数据框如下所示。

A       B
1   [{"name":"john","age":"28","salary":"50000"},{"name":"Todd","age":"36","salary":"54000"}]
2   [{"name":"Alex","age":"48","salary":"70000"},{"name":"Mark","age":"89","salary":"150000"}]
3   [{"name":"jane","age":"36","salary":"20000"},{"name":"Rose","age":"28","salary":"90000"}

如何将以下数据框转换为以下数据框

A    name   age    salary
1    john   28     50000
1    Todd   36     54000
2    Alex   48     70000
2    Mark   89     150000
3    jane   36     20000
3    Rose   28     90000

1 个答案:

答案 0 :(得分:1)

您正在使用我之前提供的相同方法先寻找unesting

newdf=unnesting(df,['B'])
pd.concat([newdf,pd.DataFrame(newdf.pop('B').tolist(),index=newdf.index)],axis=1)
   A age  name  salary
0  1  28  john   50000
0  1  36  Todd   54000
1  2  48  Alex   70000
1  2  89  Mark  150000
2  3  36  jane   20000
2  3  28  Rose   90000

更多信息我已经附加了自定义功能,您也可以在我链接的页面中找到它

def unnesting(df, explode):
    idx=df.index.repeat(df[explode[0]].str.len())
    df1=pd.concat([pd.DataFrame({x:np.concatenate(df[x].values)} )for x in explode],axis=1)
    df1.index=idx
    return df1.join(df.drop(explode,1),how='left')

数据输入

df.B.to_dict()
{0: [{'name': 'john', 'age': '28', 'salary': '50000'}, {'name': 'Todd', 'age': '36', 'salary': '54000'}], 1: [{'name': 'Alex', 'age': '48', 'salary': '70000'}, {'name': 'Mark', 'age': '89', 'salary': '150000'}], 2: [{'name': 'jane', 'age': '36', 'salary': '20000'}, {'name': 'Rose', 'age': '28', 'salary': '90000'}]}