尝试在matplotlib中绘制pandas数据或将其柱状图时出现KeyError

2024-10-01 15:40:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我在从导入的csv文件生成基本分布直方图时遇到问题。代码适用于来自另一个csv的一组数据,但不是我感兴趣的那个,这基本上是相同的。下面是我尝试过的代码:

import pandas as pd
import numpy as np
import matplotlib as plt
data = pd.read_csv("idcases.csv")
data1 = data[(data["Disease"] == "Amebiasis") & (data["County"] == "Marin")]
data2 = data[(data["Disease"] == "Amebiasis") & (data["County"] == "Sonoma")]

fig = plt.pyplot.figure()
ax = fig.add_subplot(111)
ax.hist(data1['Population'], bins =10, range = (data1['Population'].min(), data1['Population'].max()))
plt.pyplot.xlabel('Population')
plt.pyplot.ylabel('Count of Population')
plt.pyplot.show()

结果是:

^{pr2}$

我做错什么了?这是我正在阅读的数据的一部分。代码不适用于任何字段,包括“Count”或“Rate”

       Disease County  Year     Sex  Count  Population   Rate  CI.lower  \
882  Amebiasis  Marin  2001   Total     14      247731  5.651     3.090   
883  Amebiasis  Marin  2001  Female      0      125414  0.000     0.000   
884  Amebiasis  Marin  2001    Male      0      122317  0.000     0.000   
885  Amebiasis  Marin  2002   Total      7      247382  2.830     1.138   
886  Amebiasis  Marin  2002  Female      0      125308  0.000     0.000   
887  Amebiasis  Marin  2002    Male      0      122074  0.000     0.000   
888  Amebiasis  Marin  2003   Total      9      247280  3.640     1.664   
889  Amebiasis  Marin  2003  Female      0      125259  0.000     0.000   
890  Amebiasis  Marin  2003    Male      0      122021  0.000     0.000   

Tags: csv代码importdataascountplttotal
2条回答

在从matploblib-v1.4.3升级到matplotlib-v1.5.0时,我注意到pandas.Series的绘图停止工作,例如:

ax.plot_date(df['date'], df['raw'], '.-', label='raw')

将导致KeyError: 0异常。在

快速解决方案:

您需要将numpy.ndarray而不是pandas.Series传递给plot_date函数:

^{pr2}$


更多详细信息:

让我们看看异常的完整回溯:

# ... PREVIOUS TRACEBACK MESSAGES OMITTED FOR BREVITY ...

C:\Users\pedromdu\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\dates.py in default_units(x, axis)
   1562 
   1563         try:
-> 1564             x = x[0]
   1565         except (TypeError, IndexError):
   1566             pass

C:\Users\pedromdu\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    555     def __getitem__(self, key):
    556         try:
 > 557             result = self.index.get_value(self, key)
    558 
    559             if not np.isscalar(result):

C:\Users\pedromdu\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\index.py in get_value(self, series, key)
   1788 
   1789         try:
-> 1790             return self._engine.get_value(s, k)
   1791         except KeyError as e1:
   1792             if len(self) > 0 and self.inferred_type in ['integer','boolean']:

pandas\index.pyx in pandas.index.IndexEngine.get_value (pandas\index.c:3204)()

pandas\index.pyx in pandas.index.IndexEngine.get_value (pandas\index.c:2903)()

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:3843)()

pandas\hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:6525)()

pandas\hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:6463)()

KeyError: 0

请注意,当matploblib尝试执行x=x[0]时,错误就会产生。如果pandas系列没有使用从零开始的整数编制索引,则此操作将失败,因为这将查找索引值为0的项,而不是查找pandas.Series0th元素。在

要解决这个问题,我们需要从pandas.Series中的数据中获得numpy.ndarray,然后用它来绘制:

^{pr2}$

我的阴谋:

import io
import matplotlib.pyplot as plt


s = """       Disease County  Year     Sex  Count  Population   Rate  CI.lower
 Amebiasis  Marin  2001   Total     14      247731  5.651     3.090   
 Amebiasis  Marin  2001  Female      0      125414  0.000     0.000   
Amebiasis  Marin  2001    Male      0      122317  0.000     0.000   
Amebiasis  Marin  2002   Total      7      247382  2.830     1.138   
Amebiasis  Marin  2002  Female      0      125308  0.000     0.000   
 Amebiasis  Marin  2002    Male      0      122074  0.000     0.000   
Amebiasis  Marin  2003   Total      9      247280  3.640     1.664   
Amebiasis  Marin  2003  Female      0      125259  0.000     0.000   
 Amebiasis  Marin  2003    Male      0      122021  0.000     0.000  """
fobj = io.StringIO(s)
data1 = pd.read_csv(fobj, delim_whitespace=True)
plt.hist(data1['Population'], bins =10, range = (data1['Population'].min(), data1['Population'].max()))
plt.xlabel('Population')
plt.ylabel('Count of Population')
plt.show()

enter image description here

相关问题 更多 >

    热门问题