python - Pandas KeyError: Year for csv file dataframe -


i have dataframe similar tot one:

 birthyear    sex    area    count 2015         w      dhaka    6 2015         m      dhaka    3 2015         w      khulna   1 2015         m      khulna   8 2014         m      dhaka    13 2014         w      dhaka    20 2014         m      khulna   9 2014         w      khulna   6 2013         w      dhaka    11 2013         m      dhaka    2 2013         w      khulna    8 2013         m      khulna    5 2012         m      dhaka    12 2012         w      dhaka    4 2012         w      khulna    7 2012         m      khulna    1 

now want create barchart in pandas male & female born on 2015 shown. code :

df = pd.read_csv('out.csv') df=df.reset_index() df=df.loc[df["birthyear"]==2015] agg_df = df.groupby(['sex']).sum() agg_df.reset_index(inplace=true) piv_df = agg_df.pivot(columns='sex', values='count') piv_df.plot.bar(stacked=true) plt.show() 

and after execution,idle shows error:

    traceback (most recent call last):   file "c:\users\sabid\appdata\local\programs\python\python35\lib\site-packages\pandas\indexes\base.py", line 1945, in get_loc     return self._engine.get_loc(key)   file "pandas\index.pyx", line 137, in pandas.index.indexengine.get_loc (pandas\index.c:4066)   file "pandas\index.pyx", line 159, in pandas.index.indexengine.get_loc (pandas\index.c:3930)   file "pandas\hashtable.pyx", line 675, in pandas.hashtable.pyobjecthashtable.get_item (pandas\hashtable.c:12408)   file "pandas\hashtable.pyx", line 683, in pandas.hashtable.pyobjecthashtable.get_item (pandas\hashtable.c:12359) keyerror: 'birthyear'  during handling of above exception, exception occurred:  traceback (most recent call last):   file "c:/users/sabid/dropbox/freelancing/data visualization python/pie.py", line 8, in <module>     df=df.loc[df["stichtagdatjahr"]==2015]   file "c:\users\sabid\appdata\local\programs\python\python35\lib\site-packages\pandas\core\frame.py", line 1997, in __getitem__     return self._getitem_column(key)   file "c:\users\sabid\appdata\local\programs\python\python35\lib\site-packages\pandas\core\frame.py", line 2004, in _getitem_column     return self._get_item_cache(key)   file "c:\users\sabid\appdata\local\programs\python\python35\lib\site-packages\pandas\core\generic.py", line 1350, in _get_item_cache     values = self._data.get(item)   file "c:\users\sabid\appdata\local\programs\python\python35\lib\site-packages\pandas\core\internals.py", line 3290, in     loc = self.items.get_loc(item)   file "c:\users\sabid\appdata\local\programs\python\python35\lib\site-packages\pandas\indexes\base.py", line 1947, in get_loc     return self._engine.get_loc(self._maybe_cast_indexer(key))   file "pandas\index.pyx", line 137, in pandas.index.indexengine.get_loc (pandas\index.c:4066)   file "pandas\index.pyx", line 159, in pandas.index.indexengine.get_loc (pandas\index.c:3930)   file "pandas\hashtable.pyx", line 675, in pandas.hashtable.pyobjecthashtable.get_item (pandas\hashtable.c:12408)   file "pandas\hashtable.pyx", line 683, in pandas.hashtable.pyobjecthashtable.get_item (pandas\hashtable.c:12359) keyerror: 'birthyear' 

i came know this link happens because 'birthyear' column name has header before it. don't know how remove header , make code work. there fruitful solution this?

you can rename columns.

df.rename(columns=["birthyear", "sex", "area", "count"], inplace=true) 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -