SUMLEV REGION DIVISION ... RNETMIG2013 RNETMIG2014 RNETMIG2015 0 40 3 6 ... 1.383282 1.724718 0.712594 1 50 3 6 ... -2.722002 2.592270 -2.187333 2 50 3 6 ... 22.727626 20.317142 18.293499 3 50 3 6 ... -7.167664 -3.978583 -10.543299 4 50 3 6 ... -5.403729 0.754533 1.107861
import pandas as pd
import numpy as np
import timeit
df=pd.read_csv("census.csv")
print(df.head())
def second_approach():
global df
new_df=df[["SUMLEV"]==50]
new_df.set_index(["STNAME","CTYNAME"],inplace=True)
return (new_df.rename(columns={"ESTIMATESBASE2010":"Estimate Base 2010"}))
print(timeit.timeit(second_approach,number=10))
I can't detect what is getting wrong.Can anybody help? this is the error I am getting: Traceback (most recent call last):
File C:\ProgramData\anaconda3\lib\site-packages\pandas\core\indexes\base.py:3802 in get_loc return self._engine.get_loc(casted_key)
File pandas_libs\index.pyx:138 in pandas._libs.index.IndexEngine.get_loc
File pandas_libs\index.pyx:165 in pandas._libs.index.IndexEngine.get_loc
File pandas_libs\hashtable_class_helper.pxi:5745 in pandas._libs.hashtable.PyObjectHashTable.get_item
File pandas_libs\hashtable_class_helper.pxi:5753 in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: False
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File C:\ProgramData\anaconda3\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec exec(code, globals, locals)
File c:\users\96399\idioms.py:14 print(timeit.timeit(second_approach,number=10))
File C:\ProgramData\anaconda3\lib\timeit.py:234 in timeit return Timer(stmt, setup, timer, globals).timeit(number)
File C:\ProgramData\anaconda3\lib\timeit.py:178 in timeit timing = self.inner(it, self.timer)
File :6 in inner
File c:\users\96399\idioms.py:11 in second_approach new_df=df[["SUMLEV"]==50]
File C:\ProgramData\anaconda3\lib\site-packages\pandas\core\frame.py:3807 in getitem indexer = self.columns.get_loc(key)
File C:\ProgramData\anaconda3\lib\site-packages\pandas\core\indexes\base.py:3804 in get_loc raise KeyError(key) from err
KeyError: False