Python 必须使用某种集合调用索引:将列名分配给dataframe
我有如下的Python 必须使用某种集合调用索引:将列名分配给dataframe,python,pandas,dataframe,Python,Pandas,Dataframe,我有如下的reweightTarget,我想把它转换成一个数据帧。但是,我得到了以下错误: TypeError:必须使用某种集合调用索引(…), “t”通过了 如果我删除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-0
reweightTarget
,我想把它转换成一个数据帧。但是,我得到了以下错误:
TypeError:必须使用某种集合调用索引(…),
“t”通过了
如果我删除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
重新称重目标
交易日期
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
数据类型:64
pd.DataFrame(重新加权目标,列='t')
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 pd.DataFrame(重新加权目标,columns='t')
C:\Anaconda3\lib\site packages\pandas\core\frame.py in\uuuuuu init\uuuuuuu(self、data、index、columns、dtype、copy)
253其他:
254 mgr=self.\u init\u ndarray(数据、索引、列、数据类型=数据类型、,
-->255拷贝=拷贝)
256 elif isinstance(数据,(列表,类型.GeneratorType)):
257如果存在(数据、类型、生成器类型):
C:\Anaconda3\lib\site packages\pandas\core\frame.py in_init_ndarray(self、value、index、columns、dtype、copy)
421带回溯的raise_(e)
422
-->423索引,列=_获取_轴(*values.shape)
424值=值。T
425
C:\Anaconda3\lib\site packages\pandas\core\frame.py in\u get\u轴(N、K、索引、列)
388列=_默认值_索引(K)
389其他:
-->390列=_确保_索引(列)
391返回索引,列
392
C:\Anaconda3\lib\site packages\pandas\index\base.py in\u sure\u index(类索引,复制)
3407类索引=复制(类索引)
3408
->3409回报指数(类似指数)
3410
3411
C:\Anaconda3\lib\site packages\pandas\index\base.py in\uuuuuu new\uuuuuuuuu(cls、数据、数据类型、副本、名称、快速路径、tupleize\u cols、**kwargs)
266**夸尔格)
267 elif数据为None或lib.isscalar(数据):
-->268 cls.\标量\数据\错误(数据)
269其他:
270 if(tupleize_cols和isinstance(数据、列表)和数据和
C:\Anaconda3\lib\site packages\pandas\index\base.py in\u scalar\u data\u错误(cls,data)
481 raise TypeError(“{0}(…”)必须使用一些
482“种类,{1}已传递”。格式(cls.\uuuuuuu name\uuuuuuuu),
-->483报告(数据)))
484
485@classmethod
TypeError:必须使用某种集合调用索引(…),传递了“t”
文档:
列:索引或类似数组的
用于生成框架的列标签。如果未提供列标签,则默认为np.arange(n)
例如:
df3=数据帧(np.random.randn(10,5),列=['a','b','c','d','e'])
尝试使用:
pd.DataFrame(reweightTarget, columns=['t'])
当您想在数据框中设置
索引
或列
时,您应该在列表
中定义它
pd.DataFrame(reweightTarget, columns=list('t'))
只需将
columns='t'
更改为columns='t']
。您还可以执行reweightTarget.to_frame('t')