I'm trying to create a sunburst plot where different rows have different lengths, and get the error message 'dtype: object, 'is not a leaf.' I have read this 'Note that the parents of None entries must be a leaf, i.e. it cannot have other children than None (otherwise a ValueError is raised).' on the plotly pages https://plotly.com/python/sunburst-charts/#rectangular-data-with-missing-values but don't fully understand that.
I have a bigger dataset, but the same thing happens with this one:
testdf = pd.DataFrame(
[['Max',10,'M','a', 'x',None],
['Ma',5, 'M', 'a', None,None],
['Johan',6, 'J','o','h','a']],
index=[1, 2, 3],
columns=['First_Name','Count','a', 'b', 'c','d'])
testdf
fig=px.sunburst(testdf,path=['a','b','c','d'],values='Count')
fig.show()
The ValueError is this:
ValueError: ('Non-leaves rows are not permitted in the dataframe \n', a M b a c
d
Name: 1, dtype: object, 'is not a leaf.')
So I think it's caused by the fact that the letter a is not a leaf since the x of the first row is also attached to it, but I would like to have the sunburst stop at the letter a for the second row, and at the letter x for the first row. Any help is greatly appreciated!
This is explained here: https://community.plotly.com/t/sunburst-chart-cant-handle-none-values/35383.
Basically, if you have a None
value, its parents have to be unique. In your example, the first row is valid. The second isn't because ["M", "a", None, None]
shares the same parent as ["M", "a", "x", None]
.
In fact, if you run the below, it works.
testdf = pd.DataFrame([
['Max', 10, 'M', 'a', 'x', None],
['Ma', 5, 'M', 'a', 'd', None],
['Johan', 6, 'J', 'o', 'h', 'a']],
index=[1, 2, 3],
columns=['First_Name', 'Count', 'a', 'b', 'c', 'd'])
testdf
fig = px.sunburst(testdf, path=['a', 'b', 'c', 'd'], values='Count')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With