KeyError:使用数据透视表时出现“符号”

时间:2019-12-02 04:16:51

标签: python python-3.x pandas pivot-table

我试图在pandas数据框中查找数据:

tidyr::complete(DATA, datetime = seq(min(datetime), max(datetime), by = "1 min"))

如果我在这里拆开数据

$data = array(
        'harga_jual' => $this->input->post('harga_jual') == '' ? NULL : $this->input->post('harga_jual')
        );

我得到以下信息:

import pandas as pd
import numpy as np
from statsmodels import api as sm
import pandas_datareader.data as web
import datetime
start = datetime.datetime(2016,12,2)
end = datetime.datetime.today()

df = web.get_data_yahoo(['F', '^GSPC'], start, end)

df具有以下数据:

df.unstack()

要在df中查找数据,我正在使用数据透视表:

Attributes  Symbols  Date      
Adj Close   F        2016-12-01    1.011866e+01
                     2016-12-02    9.963994e+00
                     2016-12-05    1.012680e+01
                     2016-12-06    1.022449e+01
                     2016-12-07    1.063152e+01
                                       ...     
Volume      ^GSPC    2019-11-22    3.226780e+09
                     2019-11-25    3.511530e+09
                     2019-11-26    4.595590e+09
                     2019-11-27    3.033090e+09
                     2019-11-29    1.743020e+11
Length: 9048, dtype: float64

但是我遇到一个错误:

Attributes  Adj Close   Close   High    Low Open    Volume
Symbols F   ^GSPC   F   ^GSPC   F   ^GSPC   F   ^GSPC   F   ^GSPC   F   ^GSPC
Date                                                
2015-02-11  12.216836   2068.530029 16.250000   2068.530029 16.309999   2073.479980 16.010000   2057.989990 16.080000   2068.550049 34285300.0  3.596860e+09
2015-02-12  12.299535   2088.479980 16.360001   2088.479980 16.450001   2088.530029 16.299999   2069.979980 16.340000   2069.979980 23738800.0  3.788350e+09
2015-02-13  12.254424   2096.989990 16.299999   2096.989990 16.360001   2097.030029 16.190001   2086.699951 16.330000   2088.780029 19954600.0  3.527450e+09
2015-02-17  12.111583   2100.340088 16.110001   2100.340088 16.299999   2101.300049 16.000000   2089.800049 16.209999   2096.469971 44362300.0  3.361750e+09
2015-02-18  12.186762   2099.679932 16.209999   2099.679932 16.330000   2100.229980 16.059999   2092.149902 16.160000   2099.159912 22812700.0  3.370020e+09
... ... ... ... ... ... ... ... ... ... ... ... ...
2019-11-22  8.890000    3110.290039 8.890000    3110.290039 8.900000    3112.870117 8.770000    3099.260010 8.800000    3111.409912 34966700.0  3.226780e+09
2019-11-25  9.000000    3133.639893 9.000000    3133.639893 9.010000    3133.830078 8.870000    3117.439941 8.900000    3117.439941 30580900.0  3.511530e+09
2019-11-26  9.010000    3140.520020 9.010000    3140.520020 9.020000    3142.689941 8.910000    3131.000000 8.980000    3134.850098 30093800.0  4.595590e+09
2019-11-27  9.100000    3153.629883 9.100000    3153.629883 9.150000    3154.260010 9.020000    3143.409912 9.030000    3145.489990 37396100.0  3.033090e+09
2019-11-29  9.060000    3140.979980 9.060000    3140.979980 9.100000    3150.300049 9.030000    3139.340088 9.040000    3147.179932 13096200.0  1.743020e+11
1210 rows × 12 columns

为什么会出现此错误?

1 个答案:

答案 0 :(得分:1)

似乎您已经有了满足您需求的多索引,而不必进行调整。

>>> df['Adj Close'].head()
Symbols             F        ^GSPC
Date                              
2016-12-01  10.297861  2191.080078
2016-12-02  10.140451  2191.949951
2016-12-05  10.306145  2204.709961
2016-12-06  10.405562  2212.229980
2016-12-07  10.819797  2241.350098
>>>