如何将嵌套字典转换为数据帧?

时间:2017-04-09 10:26:25

标签: python pandas dictionary dataframe

我有一个嵌套字典。这是纳斯达克的一些数据。像这样:

{'CLSN':     
 Date        Open  High   Low  Close  Volume  Adj Close                                                
 2015-12-31  1.92  1.99  1.87   1.92   79600       1.92
 2016-01-04  1.93  1.99  1.87   1.93   39700       1.93
 2016-01-05  1.89  1.94  1.85   1.90   50200       1.90,
 'CCC':    
 Date            Open       High        Low      Close  Volume  Adj Close                                                              
 2015-12-31  17.270000  17.389999  17.120001  17.250000  177200  16.965361
 2016-01-04  17.000000  17.219999  16.600000  17.180000  371600  16.896516
 2016-01-05  17.190001  17.530001  17.059999  17.450001  417500  17.162061,
}

为了帮助您理解,后跟数据框

在询问之前,我尝试了pd.Panel(nas)['CLSN']的方式,所以我确定它的值是数据帧。但pd.Panel(nas).to_frame().reset_index()的方式根本不能帮助我!它输出一个空数据框,其中包含数千个用库存名称填充的列。

现在它很烦,我想要一个像这样的数据框:

index  Date      Open       High       Low       Close      Volume     Adj Close                                            CLSN 2015-12-31  1.92       1.99       1.87       1.92       79600.0   1.92
CLSN 2016-01-01   NaN       NaN        NaN        NaN        NaN       NaN
ClSN 2016-01-04  1.93       1.99       1.87       1.93       39700.0   1.93  
CCC  2015-12-31  17.270000  17.389999  17.120001  17.250000  177200.0  16.965361
CCC  2016-01-04  17.000000  17.219999  16.600000  17.180000  371600.0  16.896516
CCC  2016-01-05  17.190001  17.530001  17.059999  17.450001  417500.0  17.162061

当然,我可以使用for循环来获取每个股票的数据框,但它会让我加入它们。

你有更好的主意吗?非常愿意知道!

到MaxU: 使用方法print(nas['CLSN'].head())后,输出如下:

            Open  High   Low  Close  Volume  Adj Close
Date                                                  
2015-12-31  1.92  1.99  1.87   1.92   79600       1.92
2016-01-04  1.93  1.99  1.87   1.93   39700       1.93
2016-01-05  1.89  1.94  1.85   1.90   50200       1.90
2016-01-06  1.86  1.89  1.77   1.78   62100       1.78
2016-01-07  1.75  1.80  1.75   1.77  117000       1.77

2 个答案:

答案 0 :(得分:3)

<强>更新

假设Date是索引(不是常规列):

源词典:

In [70]: d2
Out[70]:
{'CCC':                  Open       High        Low      Close  Volume  Adj Close
 Date
 2015-12-31  17.270000  17.389999  17.120001  17.250000  177200  16.965361
 2016-01-04  17.000000  17.219999  16.600000  17.180000  371600  16.896516
 2016-01-05  17.190001  17.530001  17.059999  17.450001  417500  17.162061,
 'CLSN':             Open  High   Low  Close  Volume  Adj Close
 Date
 2015-12-31  1.92  1.99  1.87   1.92   79600       1.92
 2016-01-04  1.93  1.99  1.87   1.93   39700       1.93
 2016-01-05  1.89  1.94  1.85   1.90   50200       1.90}

解决方案:

In [73]: pd.Panel(d2).swapaxes(0, 2).to_frame().reset_index(level=0).sort_index()
Out[73]:
            Date       Open       High        Low      Close    Volume  Adj Close
minor
CCC   2015-12-31  17.270000  17.389999  17.120001  17.250000  177200.0  16.965361
CCC   2016-01-04  17.000000  17.219999  16.600000  17.180000  371600.0  16.896516
CCC   2016-01-05  17.190001  17.530001  17.059999  17.450001  417500.0  17.162061
CLSN  2015-12-31   1.920000   1.990000   1.870000   1.920000   79600.0   1.920000
CLSN  2016-01-04   1.930000   1.990000   1.870000   1.930000   39700.0   1.930000
CLSN  2016-01-05   1.890000   1.940000   1.850000   1.900000   50200.0   1.900000

或者您可以将Date作为索引的一部分:

In [74]: pd.Panel(d2).swapaxes(0, 2).to_frame().sort_index()
Out[74]:
                       Open       High        Low      Close    Volume  Adj Close
Date       minor
2015-12-31 CCC    17.270000  17.389999  17.120001  17.250000  177200.0  16.965361
           CLSN    1.920000   1.990000   1.870000   1.920000   79600.0   1.920000
2016-01-04 CCC    17.000000  17.219999  16.600000  17.180000  371600.0  16.896516
           CLSN    1.930000   1.990000   1.870000   1.930000   39700.0   1.930000
2016-01-05 CCC    17.190001  17.530001  17.059999  17.450001  417500.0  17.162061
           CLSN    1.890000   1.940000   1.850000   1.900000   50200.0   1.900000

OLD回答 - 它假定Date是常规列(不是索引) 试试这个:

In [59]: pd.Panel(d).swapaxes(0, 2).to_frame().reset_index('major', drop=True).sort_index()
Out[59]:
            Date   Open   High    Low  Close  Volume Adj Close
minor
CCC   2015-12-31  17.27  17.39  17.12  17.25  177200   16.9654
CCC   2016-01-04     17  17.22   16.6  17.18  371600   16.8965
CCC   2016-01-05  17.19  17.53  17.06  17.45  417500   17.1621
CLSN  2015-12-31   1.92   1.99   1.87   1.92   79600      1.92
CLSN  2016-01-04   1.93   1.99   1.87   1.93   39700      1.93
CLSN  2016-01-05   1.89   1.94   1.85    1.9   50200       1.9

其中dnested dictionary

In [60]: d
Out[60]:
{'CCC':         Date       Open       High        Low      Close  Volume  Adj Close
 0 2015-12-31  17.270000  17.389999  17.120001  17.250000  177200  16.965361
 1 2016-01-04  17.000000  17.219999  16.600000  17.180000  371600  16.896516
 2 2016-01-05  17.190001  17.530001  17.059999  17.450001  417500  17.162061,
 'CLSN':         Date  Open  High   Low  Close  Volume  Adj Close
 0 2015-12-31  1.92  1.99  1.87   1.92   79600       1.92
 1 2016-01-04  1.93  1.99  1.87   1.93   39700       1.93
 2 2016-01-05  1.89  1.94  1.85   1.90   50200       1.90}

答案 1 :(得分:2)

pandas.concat或许正在寻找:

$all = array();

$all[] = $rows1;

foreach($rows2 as $k2=>$v2) {
     $new=array();
     $new['CLIENTID'] = $v2['ClientId'];
     $new['CLIENTNAME'] = $v2['ClientName'];
     $new['PHONENO'] = $v2['PhoneNo'];
     $new['ADDRESS'] = $v2['Address'];
     $new['ROUTE'] = $v2['Route'];
     $all[]=$new;
}

 $str='<table><tr><th>CLIENTID</th><th>CLIENTNAME</th><th>PHONENO</th><th>ADDRESS</th><th>ROUTE</th></tr>';
foreach($all as $arr) {
     $str.='<tr><td>'.$arr['CLIENTID'].'</td><td>'.$arr['CLIENTNAME'].'</td><td>'.$arr['PHONENO'].'</td><td>'.$arr['ADDRESS'].'</td><td>'.$arr['ROUTE'].'</td>';
}
$str.='</table>';

echo $str;