Sunburst Chart - Can't handle None values

Having an issue getting this to plot without an error, it seems to have issues whenever I add None values.
The guide https://plot.ly/python/sunburst-charts/ (Rectangular data with missing values) seems to work fine, but I can’t recreate this with my own data

   weekday Satisfaction Score Offered Satisfaction Score  total
0      Fri                Not Offered               None    160
1      Fri                    Offered               None    102
2      Fri                    Offered               Good     24
3      Fri                    Offered                Bad     11
4      Mon                Not Offered               None    148
5      Mon                    Offered               None    126
6      Mon                    Offered               Good     17
7      Mon                    Offered                Bad     12
8      Sat                Not Offered               None     46
9      Sat                    Offered               None     54
10     Sat                    Offered               Good     13
11     Sun                Not Offered               None     31
12     Sun                    Offered               None     32
13     Sun                    Offered               Good      5
14     Sun                    Offered                Bad      2
15     Thu                Not Offered               None    171
16     Thu                    Offered               None    105
17     Thu                    Offered               Good     24
18     Thu                    Offered                Bad      9
19     Tue                Not Offered               None    169
20     Tue                    Offered               None    121
21     Tue                    Offered               Good     21
22     Tue                    Offered                Bad      5
23     Wed                Not Offered               None    160
24     Wed                    Offered               None    117
25     Wed                    Offered               Good     28
26     Wed                    Offered                Bad     10

This is the table I’m trying to create a sunburst chart with

Here is my code

satscoredata = csdata.groupby(["weekday", "Satisfaction Score Offered"])["Satisfaction Score"].value_counts().reset_index(name="total")

satscoredata["Satisfaction Score"] = satscoredata["Satisfaction Score"].replace({"Not Offered": None,
                                                                                 "Offered": None})
print(satscoredata)

fig = px.sunburst(satscoredata, 
                  path=['weekday', 'Satisfaction Score Offered', "Satisfaction Score"],
                 values="total")

fig.update_layout(title=f'Satisfaction Score By Weekday')

fig.show()

And here is the error

ValueError: ('Non-leaves rows are not permitted in the dataframe \n', weekday                           Fri
Satisfaction Score Offered    Offered
Satisfaction Score                   
Name: 1, dtype: object, 'is not a leaf.')

Thanks

Hi @leonmate welcome to the forum! Yes, as explained in this example, when you have a None value in the last path column, its parent must be a leaf, ie its parents cannot have other children than None. So the “Not offered/None” is ok but not “Offered/None” because “Offered” has other children. Could replace these offending Nones (children of Offered) with “N/A” for example?

Ah I see - that has worked for me now. Many thanks!

1 Like