0

Here is the snippet of my code.

import pandas as pd
import os
dpath = '//xxx//HEM'
for filename in os.listdir('//xxx//HEM'):
    df = pd.read_csv(dpath + '/' + filename)
    df = df['ab':'af'] #select required columns based on your requirement.
    df["ab"] = pd.to_numeric(df["ab"]) # convert datatype of the column based on your need
    df["af"] = pd.to_numeric(df["af"]) # convert datatype of the column based on your need
    df1.append(df)
    del df

df1.to_excel('test.xlsx')

On each CSV sheet in the folder i am reading from, Column AB & AF should be numeric values. I get the following error.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-c4c944eb874e> in <module>
      4 for filename in os.listdir('//xxx//HEM'):
      5     df = pd.read_csv(dpath + '/' + filename)
----> 6     df = df['ab':'af'] #select required columns based on your requirement.
      7     df["ab"] = pd.to_numeric(df["ab"]) # convert datatype of the column based on your need
      8     df1.append(df)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2906 
   2907         # Do we have a slicer (on rows)?
-> 2908         indexer = convert_to_index_sliceable(self, key)
   2909         if indexer is not None:
   2910             return self._slice(indexer, axis=0)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexing.py in convert_to_index_sliceable(obj, key)
   2454     idx = obj.index
   2455     if isinstance(key, slice):
-> 2456         return idx._convert_slice_indexer(key, kind='getitem')
   2457 
   2458     elif isinstance(key, compat.string_types):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in _convert_slice_indexer(self, key, kind)
   2926             """
   2927             if self.is_integer() or is_index_slice:
-> 2928                 return slice(self._validate_indexer('slice', key.start, kind),
   2929                              self._validate_indexer('slice', key.stop, kind),
   2930                              self._validate_indexer('slice', key.step, kind))

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in _validate_indexer(self, form, key, kind)
   4708             pass
   4709         elif kind in ['iloc', 'getitem']:
-> 4710             self._invalid_indexer(form, key)
   4711         return key
   4712 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in _invalid_indexer(self, form, key)
   3065                         "indexers [{key}] of {kind}".format(
   3066                             form=form, klass=type(self), key=key,
-> 3067                             kind=type(key)))
   3068 
   3069     # --------------------------------------------------------------------

TypeError: cannot do slice indexing on <class 'pandas.core.indexes.range.RangeIndex'> with these indexers [ab] of <class 'str'>

Is there something i am doing wrong ? I am guessing its the format of the Data ?

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
Olabode
  • 25
  • 5
  • It says that `df['ab':'af']` does not work with the indexes you provide. Are you sure you get the right column names? – 9000 Jul 23 '19 at 16:27
  • Yes i am sure. But my CSV files have headers, could that be the problem ? – Olabode Jul 23 '19 at 16:28
  • If you're sure that you only need those columns, maybe try utilizing some of the optional keyword args to read_csv such as "usecols=", "headers=", and/or "header=" when you create `df` https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html – mgrollins Jul 24 '19 at 22:06
  • 1
    Ah, you need to `df1 = df[['a','b']]` <-- note the 2nd set of square brackets to use column names. Taken from https://stackoverflow.com/questions/11285613/selecting-multiple-columns-in-a-pandas-dataframe – mgrollins Jul 24 '19 at 22:09

0 Answers0