others - python -'Series' object has no attribute 'iplot'

我使用Anaconda和Jupyter notebook,python 3.


#Import Libraries


import pandas as pd


import numpy as np


import seaborn as sns


import matplotlib.pyplot as plt



#Plotly Tools


from plotly.offline import init_notebook_mode, iplot


init_notebook_mode(connected=True)


import plotly.graph_objs as go


import plotly.offline as offline


offline.init_notebook_mode()


from plotly import tools


import plotly.tools as tls


init_notebook_mode(connected=True)



#Import CSV as a Pandas Dataframe


fp = pd.read_csv("gun-violence-data_01-2013_03-2018.csv")



#Confirm that dataset was properly loaded


fp.head()



導入庫后,我稍微清理了數據並將它放入dataframe 'fp_clean ',當我嘗試plot時:


temp = fp_clean["state"].value_counts().head(30)


temp.iplot(kind='bar')



我不斷得到以下錯誤:


---------------------------------------------------------------------------


AttributeError Traceback (most recent call last)


<ipython-input-25-261d72eb2ae5> in <module>


 4 #temp.plot(kind='bar')


 5 temp = fp_clean["state"].value_counts().head(30)


----> 6 temp.iplot(kind='bar')


 7 #temp.iplot(kind='bar', xTitle = 'State name', yTitle ="# of incidents", title = 'Top States with highest number of Gun Violence', filename='Bar')


 8 #temp.plot(kind='bar')



~AppDataLocalContinuumanaconda3libsite-packagespandascoregeneric.py in __getattr__(self, name)


 4374 if self._info_axis._can_hold_identifiers_and_holds_name(name):


 4375 return self[name]


-> 4376 return object.__getattribute__(self, name)


 4377 


 4378 def __setattr__(self, name, value):



AttributeError: 'Series' object has no attribute 'iplot'



謝謝!

时间: 原作者:

這裡的temp對象是一個pandas.series對象,當未鏈接到iplot時,該對象沒有plotly方法,需要cufflinks將plotly鏈接到Pandas並添加iplot方法:


import cufflinks as cf


cf.go_offline()


cf.set_config_file(offline=False, world_readable=True)



在此之後,嘗試直接從dataframe列印:


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



如果你沒有cufflinks,用:pip install cufflinks --upgrade)

原作者:
...