从字典字典中获取数据框

时间:2019-10-13 08:00:16

标签: pandas dataframe dictionary

我有一个包含这些键和值的result_dictionary:

{"A":{"z1":[1,2,3],"z2":[3,2,2],"z3":"mary"},
"B":{"z1":[4,4,3],"z2":[2,1,1],"z3":"john","z4":[6,5,4],"z5":[2,2,2]}}

我期待将其转换为df,如下所示:

    z1    z2    z3    z4     z5
A   1     3     mary   nan   nan
A   2     2     mary   nan   nan
A   3     2     mary   nan   nan
B   4     2     john    6     2
B   4     1     john    5     2
B   3     1     john    4     2

这是我的代码:

    df = pd.DataFrame.from_dict(resulting_dictionary, orient='index').reset_index() 

    lst_col=list(df.columns)
    empty_df=pd.DataFrame()
    for nums in lst_col:
        try:
            res = pd.DataFrame({
                col: np.repeat(df[col].values, df[nums].str.len())
                for col in df.columns.drop(nums)}).assign(
                **{nums: np.concatenate(df[nums].values)})[df.columns]

        except TypeError:
            empty_df[nums] = [None] * 6

        empty_df['index'] = res['index']
        empty_df['z3'] = res['z3']
        empty_df[nums] = res[nums]
    print(empty_df)

我尝试了一些代码,但是我想我正在扩展自己以达到所需的输出。 是否有其他方法可以轻松地从result_dictionary获取df?

1 个答案:

答案 0 :(得分:1)

您可以这样做

import pandas as pd
import numpy as np

data = {"A":{"z1":[1,2,3],"z2":[3,2,2],"z3":"mary"},
"B":{"z1":[4,4,3],"z2":[2,1,1],"z3":"john","z4":[6,5,4],"z5":[2,2,2]}}

final_df = pd.DataFrame()
for i,j in data.items():
  q = pd.concat({k: pd.Series(v) for k, v in j.items()}, axis=1)
  q.index=np.repeat(i,len(j[list(j.keys())[0]]))
  q.groupby("z3").ffill()
  q['z3'] = q['z3'].ffill()
  final_df= pd.concat([final_df, q])
  

输出:

    z1  z2  z3  z4  z5
A   1   3   mary    NaN NaN
A   2   2   mary    NaN NaN
A   3   2   mary    NaN NaN
B   4   2   john    6.0 2.0
B   4   1   john    5.0 2.0
B   3   1   john    4.0 2.0

希望这会有所帮助!