Habib Ur Rehman
Habib Ur Rehman

Reputation: 51

KeyError altering columns in dataframe

I'm trying to change column or deal with columns and I'm getting some keyError error. Working on chicago crime data analysis.

For example when i'm trying to run

ds["DATE OF OCCURRENCE"] = pd.to_datetime([ds["DATE OF OCCURRENCE"]], format="%m/%d/%Y %I:%M:%S %p")

KeyError

Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)

Complete code:

import pandas as pd
url="https://data.cityofchicago.org/api/views/x2n5-8w5q/rows.csv?accessType=DOWNLOAD"
df= pd.read_csv(url)
ds = df.copy()
ds["DATE OF OCCURRENCE"] = pd.to_datetime([ds["DATE OF OCCURRENCE"]], format="%m/%d/%Y %I:%M:%S %p")

This is the Error:

2896 try: -> 2897 return self._engine.get_loc(key) 2898 except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'DATE OF OCCURRENCE'

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last) 2 frames /usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2897 return self._engine.get_loc(key) 2898 except KeyError: -> 2899 return self._engine.get_loc(self._maybe_cast_indexer(key)) 2900
indexer = self.get_indexer([key], method=method, tolerance=tolerance) 2901 if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'DATE OF OCCURRENCE'

Upvotes: 3

Views: 5976

Answers (1)

jezrael
jezrael

Reputation: 862851

Your column is renamed, so need Crime_Date and also select column only one [] for Series:

ds["Crime_Date"] = pd.to_datetime(ds["Crime_Date"], format="%m/%d/%Y %I:%M:%S %p")

EDIT:

There are some spaces in column name, so need:

ds["DATE  OF OCCURRENCE"] = pd.to_datetime(ds["DATE  OF OCCURRENCE"], format="%m/%d/%Y %I:%M:%S %p")

Upvotes: 2

Related Questions