如何使用pandas获得csv的中位数?

时间:2018-03-26 21:48:36

标签: pandas median keyerror

所以我只是在学习熊猫图书馆,我认为这不应该那么困难,而且我也不知道为什么我会被抛出一个关键错误。我可能做错了,因为我是python的初学者。但我想知道为什么我不能用以下代码找到csv的中位数?我也使用repl.it来运行此代码。

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

如果你需要我可以发布csv,如果这可能有助于调试这个。我试图获得第一列的中位数,我会按名称调用它,但该列没有标题。我查看了我的书,以及如果列没有标题,他们如何指示它被编码。抛出的关键错误是

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

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

1 个答案:

答案 0 :(得分:1)

Pandas正在寻找一个列,而census[0]将返回第一行。如果您知道自己想要第一列,可以使用df.columns[0]告诉它您想要它。

那就是census[census.columns[0]].median()