Search code examples
pythonpandasplotlysunburst-diagram

Plotly: Error with pandas dataframe: module 'plotly.express.data' has no attribute


Firstly, apologies if this is a silly question, this is my first time using plotly. I am trying to make a sunburst diagram using my 'actor' dataframe, but I get an attribute error when I attempt to do so:

Error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-32-2e5f13ef3c16> in <module>
----> 1 px.data.actor

AttributeError: module 'plotly.express.data' has no attribute 'actor'

Screenshot:

enter image description here

I have the following packages imported at the top:

import plotly.graph_objects as go
import plotly.io as pio
import plotly.express as px

Can anyone see where I'm going wrong? Thanks in advance!


Solution

  • It seems you're assuming that px.data.actor somehow would make your dataframe actor available to plotly. And I can understand why since px.data will make some built-in datasets available to you, like px.data.carshare():

        centroid_lat    centroid_lon    car_hours   peak_hour
    0   45.471549       -73.588684      1772.750000 2
    1   45.543865       -73.562456      986.333333  23
    2   45.487640       -73.642767      354.750000  20
    3   45.522870       -73.595677      560.166667  23
    4   45.453971       -73.738946      2836.666667 19
    
    [...]   
    
    244 45.547171       -73.556258      951.416667  3
    245 45.546482       -73.574939      795.416667  2
    246 45.495523       -73.627725      425.750000  8
    247 45.521199       -73.581789      1044.833333 17
    248 45.532564       -73.567535      694.916667  5
    

    To inspect all datatasets avaiable to you in the same manner, just run dir(px.data) to get:

    ['absolute_import',
     'carshare',
     'election',
     'election_geojson',
     'gapminder',
     'iris',
     'tips',
     'wind']
    

    But since actor already is available to you (because you've presumably made it yourself), the line px.data.actor() is not necessary at all.

    P.S

    Running px.express.carshare() returns a pandas dataframe. To keep working with this dataframe it's best to assign it to a variable like this: df_cs = px.data.carshare()