将自定义键添加到字典以创建熊猫数据框

时间:2019-02-25 17:43:05

标签: python-3.x pandas dictionary

我有两个这样的索引列表,

L1 = [1,1,3,5]
L2 = [0,2,3,5]

我有两个这样的数据帧,

df1:

   Name     State      Zip        combined_1
0  Auto     MN            10      Auto,MN,10 
1  Rtla     VI           253      Rtla,VI,253  
2  Huka     CO         56218      Huka,CO,56218
3  kann     PR           214      Kann,PR,214
4  Himm     NJ         65216      Himm,NJ,65216
5  Elko     NY         65418      Elko,NY,65418
6  Tasm     MA            13      Tasm,MA,13
7  Hspt     OH         43218      Hspt,OH,43218

df2:

   Name     State      Zip        combined_2
0  Kilo     NC         69521      Kilo,NC,69521 
1  Kjhl     FL          3369      Kjhl,FL,3369
2  Rtla     VI         25301      Rtla,VI,25301
3  Illt     GA         30024      Illt,GA,30024
4  Huka     CO         56218      Huka,CO,56218
5  Haja     OH         96766      Haja,OH,96766

现在,我想创建一个字典来创建一个来自两个数据框的熊猫数据框。

  From_L1          From_L2
0 Rtla,VI,253      Kilo,NC,69521 
1 Rtla,VI,253      Rtla,VI,25301
2 Kann,PR,214      Illt,GA,30024
3 Elko,NY,65418    Haja,OH,96766

所以,我尝试了这个

needed_dict = {}
for indexs, values in enumerate(L1):

    for ind, val in enumerate(L2):

        if indexs == ind:

           needed_dict['From_L1'] = df1['combined_1'].iloc[values:values+1]
           needed_dict['From_L2'] = df2['combined_2'].iloc[val:val+1]

现在,我要做的是:

final_df = pd.DataFrame(needed_dict)

但是,我对needed_dict的输出是:

{'From_L1': 5    Elko,NY,65418
 Name: combined_1, dtype: object, 'From_L2': 5    Haja,OH,96766
 Name: combined_2, dtype: object}

因此,我无法创建数据框。如何创建字典以创建最终数据框?

2 个答案:

答案 0 :(得分:2)

concatloc一起使用

yourdf = pd.concat([df1[['combined_1']].loc[L1].reset_index(drop=True),
          df2[['combined_2']].loc[L2].reset_index(drop=True)],keys=['L1','L2'],axis=1)
yourdf.columns=yourdf.columns.droplevel(1)
yourdf=yourdf.add_prefix('From_')
yourdf
Out[740]: 
         From_L1        From_L2
0    Rtla,VI,253  Kilo,NC,69521
1    Rtla,VI,253  Rtla,VI,25301
2    Kann,PR,214  Illt,GA,30024
3  Elko,NY,65418  Haja,OH,96766

答案 1 :(得分:1)

使用reindexpd.concat

df_out = pd.concat([df1.reindex(L1)['combined_1'].reset_index(drop=True),
                    df2.reindex(L2)['combined_2'].reset_index(drop=True)], 
                   axis=1, 
                   keys=['From_L1','From_L2'])

输出:

         From_L1        From_L2
0    Rtla,VI,253  Kilo,NC,69521
1    Rtla,VI,253  Rtla,VI,25301
2    Kann,PR,214  Illt,GA,30024
3  Elko,NY,65418  Haja,OH,96766