Exploring Ebay Car Sales Data executed jupyter cell twice now problem

`

autos["odometer"] = (autos["odometer"]
                         .str.replace("km","")
                         .str.replace(",","")
                         .astype(int)
                         )
autos.rename({"odometer": "odometer_km"}, axis=1, inplace=True)
autos["odometer_km"].head()

I executed the code above twice or three times in jupyter and now have problems since I am getting an errort message I tried to undo this by reversing the
content of the parameters but am unable to undo the error message I get when I try to execute the code again … Do I need to start the whole project from scratch again, please advise?

autos["odometer"] = (autos["odometer"]
                             .str.replace("","km")
                             .str.replace("",",")
                             .astype(float)
                             )
autos.rename({"odometer_km": "odo"}, axis=1, inplace=True)
autos["odo"].head()

`

KeyError                                  Traceback (most recent call last)

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
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: ‘odometer’

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last)
in
----> 1 autos[“odometer”] = (autos[“odometer”]
2 .str.replace(“km”,"")
3 .str.replace(",","")
4 .astype(int)
5 )

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in getitem(self, key)
2978 if self.columns.nlevels > 1:
2979 return self._getitem_multilevel(key)
-> 2980 indexer = self.columns.get_loc(key)
2981 if is_integer(indexer):
2982 indexer = [indexer]

~/opt/anaconda3/lib/python3.7/site-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: ‘odometer’

If you’re trying to reverse that first cell, then you will have to rename that column before you apply any changes to the column values.

Since you haven’t updated the column name odometer_km back to odometer, your following code that replaces the strings

autos["odometer"] = (autos["odometer"]
                             .str.replace("","km")
                             .str.replace("",",")
                             .astype(float)
                             )

throws the error since odometer doesn’t exist. So, you either rename the column before the above code, or you change the column name to the current one.

You don’t have to reverse things either through code. I find it often easier to just re-run the code cells from the beginning and till that particular cell which “resets” everything. You don’t have to start from scratch, because all your code cells in your Notebook would still be there, and you can just re-run those cells from the point where you load in your dataset. It doesn’t take long to re-run the cells.

1 Like

Thanks, but I tried this and still get an error:

autos["odometer_km"] = (autos["odometer"]
                             .str.replace("","km")
                             .str.replace("",",")
                             .astype(float)
                             )

this also errors:

autos[“odometer_km”] = (autos[“odometer_km”]
.str.replace("",“km”)
.str.replace("",",")
.astype(float)
)

At this point, it’s unclear how many times you have been running different Cells in your Notebook and in which order. You will likely keep facing such errors, which is difficult to provide any help for without looking at the errors and your code so far.

I would recommend only two things at this stage -

  1. Be more careful of what column names you are using and when. Also be careful of running Cells again when you are making changes to specific Columns.
  2. Instead of trying to reverse changes made to a column, just re-run your Notebook Cells from the beginning. It’s not a big project to be of any kind of problem.
1 Like

Thanks very much.
I was unaware that was a possible solution to run the cells again from the start, it all works fine now. I shall take note of the those two points and also the reason for the error - throws the error since odometer doesn’t exist…

Many thanks.

1 Like