DataFrame-列中的值是字典。如何键和值到主数据帧?

时间:2020-04-17 17:10:31

标签: python pandas dataframe dictionary

我有这个df:

  <BrowserRouter>
    <div>
      <Navbar />
      <Switch>
        <Route path="/" component={Home} exact />
        <Route path="/Aboutus" component={Aboutus} />
        <Route path="/Contactus" component={ContactUs} />



        <Route path="/selecttemplate" component={SelectT} />
        }/>
        }/>
        }/>
      </Switch>
    </div>
    <Route path="/result1" component={Result1} />
    <Route className='FullHeight' path="/result2" component={Result2} />
    <Route className='FullHeight' path="/result3" component={Result3} />
    <Route className='FullHeight' path="/result4" component={Result4} />
    <Route className='FullHeight' path="/result5" component={Result5} />
  </BrowserRouter>
);

其中的测试显示如下:

d = {'name':'CompanyABCD' , 
     'office_location':[{'office_x':'lat,long','office_y':'lat,long'}] , 
     'total_employees':100}

test = pd.DataFrame(d)

如何提取信息,以便数据框生成以下信息:

    name          office_location                            total_employees
0   CompanyABCD   {'office_x': 'lat,long', 'office_y':...    100

2 个答案:

答案 0 :(得分:0)

您可以将字典转换为数据框并melt,然后在repeating到字典的length之后将其连接/分配给数据框:

m = pd.DataFrame(test['office_location'].tolist())
           .melt(var_name='Office',value_name='LatLong')

out = (test.loc[test.index.repeat(test['office_location'].str.len())]
      .reset_index(drop=True).assign(**m))

          name                                   office_location  \
0  CompanyABCD  {'office_x': 'lat,long', 'office_y': 'lat,long'}   
1  CompanyABCD  {'office_x': 'lat,long', 'office_y': 'lat,long'}   

   total_employees    Office   LatLong  
0              100  office_x  lat,long  
1              100  office_y  lat,long  

答案 1 :(得分:0)

最简单的方法是将词典提取到简单的词典列表中,并从中构建一个辅助数据框,然后将其水平连接到原始词典:

aux = pd.DataFrame(test['office_location'].tolist()).stack().reset_index(level=1)

目前,我们有:

    level_1         0
0  office_x  lat,long
0  office_y  lat,long

建立联系的时间:

resul = pd.concat([test, aux.rename(columns={'level_1': 'OfficeName',
                 '0': 'LatLong'})], axis=1)

获得:

          name                                   office_location  total_employees OfficeName         0
0  CompanyABCD  {'office_x': 'lat,long', 'office_y': 'lat,long'}              100   office_x  lat,long
0  CompanyABCD  {'office_x': 'lat,long', 'office_y': 'lat,long'}              100   office_y  lat,long

但是我认为,当您从数据库中提取数据并且在向数据帧提供数据之前,应该对数据进行预处理。