Why I get Key error even though column present in pandas?

Viewed 837

My file contain columns as Company, RecordID, Sale etc..When i first try to find its datatype after loading file to pandas dataframe, it lists float/int for few columns. So I change them to string as follows;

data = pd.read_csv(filepath)
    print(data.dtypes)

Company Code          object
SiteCode               int64
Product Name          object
RECORD ID             int64
Tank ID                int64
Date                   int64
Sale Volume          float64
Deliveries Volume      int64
Dip Volume           float64

Then I change their type as string before writing the output to a file;

>   data['RECORD ID'] = data['RECORD ID'].astype(str)
>      data['Tank ID'] = data['Tank ID'].astype(str)

here i get;

    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'RECORD ID'

If i comment recordid and let Tankid to convert, then I get same key error for TankId, why is that?

Here is the sample csv; SIRA RECORD ID and TANK ID columns giving issue

 Company Code,SiteCode,Product Name,SIRA RECORD ID,Tank ID,Date,Sale Volume,Deliveries Volume,Dip Volume
    XXX,20995,27PMAXDSL,3535352,4,20191004,4383.49,12902,16000
    XXX,20995,02ULP,3535351,3,20191004,8221.573,15996,9987.32
    XXX,20995,02ULP,3535350,2,20191004,7303.1,8201,11200
3 Answers

If need all columns to strings better is use parameter dtype in read_csv:

data = pd.read_csv(filepath, dtype=str)

try the below code. A list of index is passed to pandas not the index directly

record_ids = data[["RECORD ID"]]

Instead of manually entering the column names, try the following, as a test:

for col in data.columns:
    data[col] = data[col].astype(str)

This should also work for the entire dataframe

data = data.astype('str')
Related