字典到pandas DataFrame

时间:2016-10-18 14:33:58

标签: python pandas dictionary dataframe

我有这本词典:

diccionario = {'Monetarios':['B1','B2'],
            'Monetario Dinamico':['B1','B2'],
            'Renta fija corto plazo':['B1','B2'],
            'Garantizados de RF':['B1','B2'],
            'Renta Fija Largo Plazo':['B2','B3'],
            'Garantizados de RV':['B2','B3'],
            'Mixtos Renta Fija':['B2','B3'],
            'Mixtos Renta Variable':['B3','B4'],
            'Renta Variable':['B3','B4'],
            'Alternativos':['B3','B4'],
            'Fondos Inmobiliarios en Directo':['G3','G3'],
            'IIC de Inversion Libre':['G4','G4'],
            'IIC de IIC de Inversion Libre':['G4','G4'],
            'Money Markets':['B1','B2'],
            'Money Markets Enhanced':['B1','B2'],
            'Fixed Income Short Term':['B1','B2'],
            'Capital Guaranteed Funds':['B1','B2'],
            'Fixed Income Long Term':['B2','B3'],
            'Capital Guaranteed Equity Funds':['B2','B3'],
            'Mixed Fixed Income Funds':['B2','B3'],
            'Mixed Equity Funds':['B3','B4'],
            'Equity':['B3','B4'],
            'Alternatives':['B3','B4'],
            'Real State':['G3','G4'],
            'Hedge Funds':['G4','G4'],
            'Funds of Hedge Funds':['G4','G4'],
            'HARMONIZED':'G4',
            'HIGH_YLD_EMERGING_MARKETS':'B4'
           }

我想要一个数据框,其中我使用的词作为第一列的键,以及分配给这些键的值作为其他列,如下所示:

col 1       col 2     col 3
Monetarios    B1        B2
Monetar din.  B1        B2
Rent fija...  B1        B2

... ...

我刚刚得到了第一个专栏:     df_dict = pd.DataFrame(diccionario)

k3 = list(df_dict.columns.values)

提前致谢

1 个答案:

答案 0 :(得分:3)

我认为您可以T使用转置reset_index

df = pd.DataFrame.from_dict(diccionario).T.reset_index()
df.columns = ['col1','col2','col3']
print (df)
                               col1 col2 col3
0                      Alternatives   B3   B4
1                      Alternativos   B3   B4
2   Capital Guaranteed Equity Funds   B2   B3
3          Capital Guaranteed Funds   B1   B2
4                            Equity   B3   B4
5            Fixed Income Long Term   B2   B3
6           Fixed Income Short Term   B1   B2
7   Fondos Inmobiliarios en Directo   G3   G3
8              Funds of Hedge Funds   G4   G4
9                Garantizados de RF   B1   B2
10               Garantizados de RV   B2   B3
11                       HARMONIZED   G4   G4
12        HIGH_YLD_EMERGING_MARKETS   B4   B4
13                      Hedge Funds   G4   G4
14    IIC de IIC de Inversion Libre   G4   G4
15           IIC de Inversion Libre   G4   G4
16               Mixed Equity Funds   B3   B4
17         Mixed Fixed Income Funds   B2   B3
18                Mixtos Renta Fija   B2   B3
19            Mixtos Renta Variable   B3   B4
20               Monetario Dinamico   B1   B2
21                       Monetarios   B1   B2
22                    Money Markets   B1   B2
23           Money Markets Enhanced   B1   B2
24                       Real State   G3   G4
25           Renta Fija Largo Plazo   B2   B3
26                   Renta Variable   B3   B4
27           Renta fija corto plazo   B1   B2