IndexError for using pandas dataframe values

Peter Otten __peter__ at web.de
Wed May 25 12:02:57 EDT 2016


Daiyue Weng wrote:

> Hi, I tried to use DataFrame.values to convert a list of columns in a
> dataframe to a numpy ndarray/matrix,
> 
> matrix = df.values[:, list_of_cols]
> 
> but got an error,
> 
> IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis
> (None) and integer or boolean arrays are valid indices
> 
> so what's the problem with the list of columns I passed in?
> 
> many thanks

Your suggestively named list_of_cols is probably not a list. Have your 
script print its value and type before the failing operation:

  print(type(list_of_cols), list_of_cols)
> matrix = df.values[:, list_of_cols]





More information about the Python-list mailing list