python-“系列”对象没有属性“ iplot”

前端之家收集整理的这篇文章主要介绍了python-“系列”对象没有属性“ iplot” 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我一直在尝试解决最近2个小时的问题,并一直在简短地介绍如何解决错误-看过youtube视频,经历了stackoverflow,但我不知道哪里出了问题.
请注意,我正在使用Anaconda和Jupyter笔记本以及Python 3进行作业.

  1. #Import Libraries
  2. import pandas as pd
  3. import numpy as np
  4. import seaborn as sns
  5. import matplotlib.pyplot as plt
  6. #Plotly Tools
  7. from plotly.offline import init_notebook_mode,iplot
  8. init_notebook_mode(connected=True)
  9. import plotly.graph_objs as go
  10. import plotly.offline as offline
  11. offline.init_notebook_mode()
  12. from plotly import tools
  13. import plotly.tools as tls
  14. init_notebook_mode(connected=True)
  15. #Import CSV as a Pandas Dataframe
  16. fp = pd.read_csv("gun-violence-data_01-2013_03-2018.csv")
  17. #Confirm that dataset was properly loaded
  18. fp.head()

导入库后,我对数据进行了一些清理,并将其放入数据帧“ fp_clean”中.当我尝试绘制时:

  1. temp = fp_clean["state"].value_counts().head(30)
  2. temp.iplot(kind='bar')

我不断收到以下错误

  1. ---------------------------------------------------------------------------
  2. AttributeError Traceback (most recent call last)
  3. <ipython-input-25-261d72eb2ae5> in <module>
  4. 4 #temp.plot(kind='bar')
  5. 5 temp = fp_clean["state"].value_counts().head(30)
  6. ----> 6 temp.iplot(kind='bar')
  7. 7 #temp.iplot(kind='bar',xTitle = 'State name',yTitle = "# of incidents",title = 'Top States with highest number of Gun Violence',filename='Bar')
  8. 8 #temp.plot(kind='bar')
  9. ~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self,name)
  10. 4374 if self._info_axis._can_hold_identifiers_and_holds_name(name):
  11. 4375 return self[name]
  12. -> 4376 return object.__getattribute__(self,name)
  13. 4377
  14. 4378 def __setattr__(self,name,value):
  15. AttributeError: 'Series' object has no attribute 'iplot'

任何帮助将不胜感激!

谢谢!

最佳答案
此处的temp对象是一个pandas.series对象,当未以图方式链接时,该对象没有iplot方法.我们需要袖扣以将其密谋链接到熊猫并添加iplot方法

  1. import cufflinks as cf
  2. cf.go_offline()
  3. cf.set_config_file(offline=False,world_readable=True)

之后,请尝试直接从数据框中绘图:

  1. fp_clean["state"].iplot(kind="bar")

(如果没有袖扣,请使用:pip install cufflinks –upgrade)

猜你在找的Python相关文章