必须使用某种集合调用索引:将列名称指定给dataframe

时间:2016-07-26 21:09:12

标签: python pandas dataframe

我有reweightTarget如下,我想将其转换为pandas Dataframe。但是,我收到了以下错误:

  

TypeError:必须使用某种集合调用Index(...),   '吨'通过了

如果我删除columns='t',则可以正常使用。任何人都可以解释一下发生了什么吗?

reweightTarget


Trading dates
2004-01-31    4.35
2004-02-29    4.46
2004-03-31    4.44
2004-04-30    4.39
2004-05-31    4.50
2004-06-30    4.53
2004-07-31    4.63
2004-08-31    4.58
dtype: float64
pd.DataFrame(reweightTarget, columns='t')


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-334-bf438351aaf2> in <module>()
----> 1 pd.DataFrame(reweightTarget, columns='t')

C:\Anaconda3\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy)
    253             else:
    254                 mgr = self._init_ndarray(data, index, columns, dtype=dtype,
--> 255                                          copy=copy)
    256         elif isinstance(data, (list, types.GeneratorType)):
    257             if isinstance(data, types.GeneratorType):

C:\Anaconda3\lib\site-packages\pandas\core\frame.py in _init_ndarray(self, values, index, columns, dtype, copy)
    421                     raise_with_traceback(e)
    422 
--> 423         index, columns = _get_axes(*values.shape)
    424         values = values.T
    425 

C:\Anaconda3\lib\site-packages\pandas\core\frame.py in _get_axes(N, K, index, columns)
    388                 columns = _default_index(K)
    389             else:
--> 390                 columns = _ensure_index(columns)
    391             return index, columns
    392 

C:\Anaconda3\lib\site-packages\pandas\indexes\base.py in _ensure_index(index_like, copy)
   3407             index_like = copy(index_like)
   3408 
-> 3409     return Index(index_like)
   3410 
   3411 

C:\Anaconda3\lib\site-packages\pandas\indexes\base.py in __new__(cls, data, dtype, copy, name, fastpath, tupleize_cols, **kwargs)
    266                          **kwargs)
    267         elif data is None or lib.isscalar(data):
--> 268             cls._scalar_data_error(data)
    269         else:
    270             if (tupleize_cols and isinstance(data, list) and data and

C:\Anaconda3\lib\site-packages\pandas\indexes\base.py in _scalar_data_error(cls, data)
    481         raise TypeError('{0}(...) must be called with a collection of some '
    482                         'kind, {1} was passed'.format(cls.__name__,
--> 483                                                       repr(data)))
    484 
    485     @classmethod

TypeError: Index(...) must be called with a collection of some kind, 't' was passed

2 个答案:

答案 0 :(得分:16)

文档:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html

  

列:索引或类似数组

     

用于结果框架的列标签。如果没有提供列标签,则默认为np.arange(n)

示例:

  

df3 = DataFrame(np.random.randn(10,5),columns = ['a','b','c','d','e'])

尝试使用:

pd.DataFrame(reweightTarget, columns=['t'])

答案 1 :(得分:0)

如果要在数据框中设置indexcolumns,则应以list的心情进行定义。

pd.DataFrame(reweightTarget, columns=list('t'))