Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.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
尝试使用Pandas和Python绘图时为空系列_Python_Python 2.7_Pandas - Fatal编程技术网

尝试使用Pandas和Python绘图时为空系列

尝试使用Pandas和Python绘图时为空系列,python,python-2.7,pandas,Python,Python 2.7,Pandas,我正试图追随韦斯·麦金尼的作品并将其完成。我已经讲到了婴儿名字的例子,我正在编写的代码和使用的代码(BabyNames.ipynb)都有同样的问题 作为参考,我在Mac(OS X 10.10.1)上使用: Python 2.7.6 IPython 2.3.1 熊猫0.15.2 我可以成功地完成所有这些: names = read_csv('baby-names2.csv') # read the data in boys = names[names.sex == 'boy']

我正试图追随韦斯·麦金尼的作品并将其完成。我已经讲到了婴儿名字的例子,我正在编写的代码和使用的代码(BabyNames.ipynb)都有同样的问题

作为参考,我在Mac(OS X 10.10.1)上使用:

  • Python 2.7.6
  • IPython 2.3.1
  • 熊猫0.15.2
我可以成功地完成所有这些:

names = read_csv('baby-names2.csv')   # read the data in
boys = names[names.sex == 'boy']      # create boys list
girls = names[names.sex == 'girl']    # create girls list

# create a function
def get_quantile_count(group, quantile=0.5):
    df = group.sort_index(by='prop', ascending=False)
    return df.prop.cumsum().searchsorted(quantile)

# call the function 
boys.groupby('year').apply(get_quantile_count)
这给了我这样的输出(为了简洁起见,只显示了一小部分数据):

然后我想绘制这些数据,如下所示:

boys.groupby('year').apply(get_quantile_count).plot()
但这给了我一个错误:

TypeError: Empty 'Series': no numeric data to plot
在观看视频时,他显示的数据在数据框中的数字周围没有方括号[]。我猜这就是造成我问题的原因


有人知道如何改变这一点吗?我当时正在看视频并自己编写代码,但如果我运行提供的笔记本BabyNames.ipynb,也会发生同样的事情。

所以我似乎发布这个问题太早了。我离开它一点,然后意识到这是一个简单的解决办法

问题是searchsorted()函数返回了一个数组,我只需要数组中的单个项。很简单。将函数修改为:

# create a function
def get_quantile_count(group, quantile=0.5):
    df = group.sort_index(by='prop', ascending=False)
    return df.prop.cumsum().searchsorted(quantile)[0]

只是使用索引0从数组中获取数字。我不知道为什么我在这件事上这么难。我猜这个函数最近一定改变了它的返回类型?或者我有没有设置错误的选项?不知道,但至少这解决了它。

我也有类似的问题,使用
.astype(float)
解决了这个问题,但您的方法可能更好

boys.groupby('year').apply(get_quantile_count).astype(float).plot()
boys.groupby('year').apply(get_quantile_count).astype(float).plot()