Index must be called with a collection of some kind: assign column name to dataframe

Question:

I have reweightTarget as follows and I want to convert it to a pandas Dataframe. However, I got following error:

TypeError: Index(…) must be called with a collection of some kind,
‘t’ was passed

If I remove columns='t', it works fine. Can anyone please explain what’s going on?

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:Anaconda3libsite-packagespandascoreframe.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:Anaconda3libsite-packagespandascoreframe.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:Anaconda3libsite-packagespandascoreframe.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:Anaconda3libsite-packagespandasindexesbase.py in _ensure_index(index_like, copy)
   3407             index_like = copy(index_like)
   3408 
-> 3409     return Index(index_like)
   3410 
   3411 

C:Anaconda3libsite-packagespandasindexesbase.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:Anaconda3libsite-packagespandasindexesbase.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
Asked By: Lisa

||

Answers:

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

columns : Index or array-like

Column labels to use for resulting frame. Will default to np.arange(n) if no column labels are provided

Example:

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

Try to use:

pd.DataFrame(reweightTarget, columns=['t'])
Answered By: Xyrus

When you want to set an index or columns in data frame you must pass it as a list, so either:

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

pd.DataFrame(reweightTarget, columns=list('t'))
Answered By: Hamed Baziyad

This "problem" (or lack of feature) was solved on Pandas 1.3.5.

It was committed on April 21st, 2021, and merged to master on October 18th, 2021. Pandas 1.3.4 was released on October 17th, 2021, so it didn’t make the cut and was left to 1.3.5.

You can read more details about this change here:

https://github.com/pandas-dev/pandas/pull/41022/commits/6afcbcd9b8915b0bef770d2bab77d6285d9a459d

https://github.com/pandas-dev/pandas/pull/41022

enter image description here

enter image description here

Answered By: Rodolfo Núñez
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.