I'm trying to rename a column and then use it, but I get the error KeyError: '[] not in index'
.
What do I need to change and why?
import pandas as pd
url = 'https://github.com/hadley/tidy-data/blob/master/data/billboard.csv'
tracks = pd.read_csv(url, encoding='latin1')
tracks.head()
tracks.columns.values[1]
tracks.columns.values[1] = "artist"
tracks.head()
tracks2 = tracks[['year', 'artist', 'track', 'time']]
tracks2.head()
Traceback
Traceback (most recent call last):
File "E:Anaconda3libsite-packagesIPythoncoreinteractiveshell.py", line 3418, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-ed116e277ed9>", line 1, in <module>
runfile('E:/PythonProjects/stack_overflow/test/test8.py', wdir='E:/PythonProjects/stack_overflow/test')
File "E:Program FilesJetBrainsPyCharm 2020.1pluginspythonhelperspydev\_pydev_bundlepydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "E:Program FilesJetBrainsPyCharm 2020.1pluginspythonhelperspydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"
", file, 'exec'), glob, loc)
File "E:/PythonProjects/stack_overflow/test/test8.py", line 15, in <module>
tracks2 = tracks[['year', 'artist', 'track', 'time']]
File "E:Anaconda3libsite-packagespandascoreframe.py", line 3030, in __getitem__
indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
File "E:Anaconda3libsite-packagespandascoreindexing.py", line 1266, in _get_listlike_indexer
self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing)
File "E:Anaconda3libsite-packagespandascoreindexing.py", line 1316, in _validate_read_indexer
raise KeyError(f"{not_found} not in index")
KeyError: '[] not in index'
Test Data
year,artist.inverted,track,time,genre,date.entered
2000,Destiny's Child,Independent Women Part I,3:38,Rock,2000-09-23
2000,Santana,"Maria, Maria",4:18,Rock,2000-02-12
2000,Savage Garden,I Knew I Loved You,4:07,Rock,1999-10-23
2000,Madonna,Music,3:45,Rock,2000-08-12
2000,"Aguilera, Christina",Come On Over Baby (All I Want Is You),3:38,Rock,2000-08-05
question from:
https://stackoverflow.com/questions/65941256/keyerror-not-in-index-when-renaming-pandas-columns-by-index-assignment