记tushare抓数据中出现的一个问题解答

今天在使用tushare抓取股票数据时有一句话出错,如下:

In [18]: df_top.loc[i,'outstanding']=basics_info[basics_info['code']==code]['outstanding']
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-d9c0353773cf> in <module>()
----> 1 df_top.loc[i,'outstanding']=basics_info[basics_info['code']==code]['outstanding']

/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.pyc in __setitem__(self, key, value)
    126     def __setitem__(self, key, value):
    127         indexer = self._get_setitem_indexer(key)
--> 128         self._setitem_with_indexer(indexer, value)
    129
    130     def _has_valid_type(self, k, axis):

/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.pyc in _setitem_with_indexer(self, indexer, value)
    385
    386             if isinstance(value, ABCSeries):
--> 387                 value = self._align_series(indexer, value)
    388
    389             info_idx = indexer[info_axis]

/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.pyc in _align_series(self, indexer, ser, multiindex_indexer)
    687             return ser.reindex(ax)._values
    688
--> 689         raise ValueError('Incompatible indexer with Series')
    690
    691     def _align_frame(self, indexer, df):

ValueError: Incompatible indexer with Series

查看一下basics_info[basics_info['code']==code]['outstanding']的属性:

In [19]: type(basics_info[basics_info['code']==code]['outstanding'])
Out[19]: pandas.core.series.Series

原来是Series!!!所以代码修改为:

df_top.loc[i,'outstanding']=basics_info[basics_info['code']==code]['outstanding'].values[0]

OK,通过!

展开阅读全文

没有更多推荐了,返回首页