Search code examples
pandasmediankeyerror

How can I get the median in a csv using pandas?


So I'm just learning the pandas library, and I think this this shouldn't be that difficult, and I don't know why I'm being thrown a Key Error. I may be doing this wrong, since I am a beginner with python. But I was wondering why I can't find the median of a csv with the following code? I'm using repl.it to run this code as well.

import pandas 
census  = pandas.read_csv("census(1).csv")
median = census[0].median()
print(median)

If you need to I can post the csv if that may help debug this. I am trying to get the median of the first column, I would call it by name, but the column doesn't have a header. I looked in my book and this how they instruct it be coded if the column doesn't have header. The Key Error thrown was

Traceback (most recent call last):
File "/tmp/.site-packages/pandas/core/indexes/base.py", line 2525, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "main.py", line 5, in <module>
median = census[0].median()
File "/tmp/.site-packages/pandas/core/frame.py", line 2139, in __getitem__
return self._getitem_column(key)
File "/tmp/.site-packages/pandas/core/frame.py", line 2146, in _getitem_column
return self._get_item_cache(key)
File "/tmp/.site-packages/pandas/core/generic.py", line 1842, in _get_item_cache
values = self._data.get(item)
File "/tmp/.site-packages/pandas/core/internals.py", line 3843, in get
loc = self.items.get_loc(item)
File "/tmp/.site-packages/pandas/core/indexes/base.py", line 2527, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

First two lines of the csv:

  39     State-gov  77516    Bachelors  13   Never-married   Adm-clerical    Not-in-family   White   Male   2174    0   40   United-States   <=50K
  50     Self-emp-not-inc   83311    Bachelors  13   Married-civ-spouse  Exec-managerial     Husband     White   Male   0   0   13   United-States   <=50K

Solution

  • Pandas is looking for a column, whereas census[0] will return the first row. If you know you want the first column, you can tell it you want that by using df.columns[0].

    So that would be census[census.columns[0]].median().