Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 子集合数据上的plt.hist错误_Python_Python 3.x_Pandas_Seaborn - Fatal编程技术网

Python 子集合数据上的plt.hist错误

Python 子集合数据上的plt.hist错误,python,python-3.x,pandas,seaborn,Python,Python 3.x,Pandas,Seaborn,我是Python新手,请原谅我的愚蠢 我正在使用matplotlib运行直方图,当我使用子集数据时会出现错误,如果我使用完整的数据集,代码将非常有效,因此我感到困惑 也许我没有正确地划分子集 下面是我的代码和相关错误,谢谢 为了提高认识,这是用Python 3编写的 导入所需的软件包: import numpy as np import matplotlib.pyplot as plt import seaborn as sns 读取数据: mlb=pd.read_csv('C:\Us

我是Python新手,请原谅我的愚蠢

我正在使用matplotlib运行直方图,当我使用子集数据时会出现错误,如果我使用完整的数据集,代码将非常有效,因此我感到困惑

也许我没有正确地划分子集

下面是我的代码和相关错误,谢谢

  • 为了提高认识,这是用Python 3编写的
导入所需的软件包:

import numpy as np

import matplotlib.pyplot as plt

import seaborn as sns
读取数据:

mlb=pd.read_csv('C:\Users\ocmh\Desktop\Python\Batting.csv')
mlb.head()
查看数据示例:

mlb=pd.read_csv('C:\Users\ocmh\Desktop\Python\Batting.csv')
mlb.head()
将数据子集以仅返回波士顿数据:

mlb_bos=mlb[(mlb['teamID'] == 'BOS')]
查看子集数据的示例:

mlb_bos.head()
绘制原始数据的柱状图:效果很好

plt.hist(mlb.AB.dropna, color= sns.desaturate("indianred",1))
绘制子集数据的直方图:返回错误(错误如下)

如果您没有安装seaborn软件包,您可以直接删除color=sns.desaturate(“indianred”,1),因为这纯粹是为了美观

错误如下:


keyrerror回溯(最近一次调用)
在()
---->1 plt.历史(mlb_bos.AB,颜色=颜色)
/hist中的Users/mattoconnell/anaconda/lib/python3.4/site-packages/matplotlib/pyplot.py
2894 histtype=histtype,align=align,orientation=orientation,
2895 rwidth=rwidth,log=log,color=color,label=label,
->2896堆叠=堆叠,**千克)
2897 draw_if_interactive()
2898最后:
/Users/mattoconnell/anaconda/lib/python3.4/site-packages/matplotlib/axes//u axes.py(self、x、bin、range、normed、weights、cumulative、bottom、histtype、align、orientation、rwidth、log、color、label、stacked、**kwargs)
5602#按摩“x”进行处理。
5603#注意:确保此处对“重量”的任何更改也在下面进行
->5604如果存在(x,np.ndarray)或不可替代(x[0]):
5605#待办事项:支持屏蔽阵列;
5606 x=np.asarray(x)
/Users/mattoconnell/anaconda/lib/python3.4/site-packages/pandas/core/series.py in_u____getitem_u_(self,key)
512 def_uuugetItem_uuuuu(自身,密钥):
513尝试:
-->514结果=self.index.get_值(self,key)
515
516如果不是np.isscalar(结果):
/获取值(self、series、key)中的Users/mattoconnell/anaconda/lib/python3.4/site-packages/pandas/core/index.py
1458
1459尝试:
->1460返回自引擎。获取值(s,k)
1461除键错误为e1外:
1462如果len(self)>0且self.u键入['integer','boolean']:
pandas.index.IndexEngine.get_值中的pandas/index.pyx(pandas/index.c:3113)()
pandas.index.IndexEngine.get_值中的pandas/index.pyx(pandas/index.c:2844)()
pandas/index.pyx在pandas.index.IndexEngine.get_loc(pandas/index.c:3704)()
pandas.hashtable.Int64HashTable.get_项中的pandas/hashtable.pyx(pandas/hashtable.c:7255)()
pandas.hashtable.Int64HashTable.get_项中的pandas/hashtable.pyx(pandas/hashtable.c:7193)()
关键错误:0

这不应该是
mlb_bos=mlb[mlb['teamID']='bos']
在子集设置后,您可能必须使用
plt.hist(mlb_bos.AB.values,color=color)
来避免此异常。谢谢。一旦我可以登录,我将尝试这两个建议