Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Series' object has no attribute 'iplot'

I have been trying to figure this problem out for the last 2 hours and keep coming up short on how to solve this error - have viewed youtube videos, went through stackoverflow, and I cannot understand where this is going wrong. Please note I am working on an assignment, using Anaconda and Jupyter notebooks, with 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()

After importing the libraries, I cleaned up the data a bit and put it into the dataframe 'fp_clean'. When I try to plot:

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

I keep getting the following error:

---------------------------------------------------------------------------
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')

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.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'

Any help would be greatly appreciated!

Thank you!

like image 388
Yasmin Avatar asked Mar 12 '19 23:03

Yasmin


1 Answers

The temp object here is a pandas.series object which does not have a iplot method when not linked to plotly. We need cufflinks to link plotly to pandas and add the iplot method:

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

After this, try plotting directly from the dataframe:

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

( If you don't have cufflinks get it with : pip install cufflinks --upgrade)

like image 118
Jesse Reza Khorasanee Avatar answered Nov 13 '22 07:11

Jesse Reza Khorasanee