title: How to structure a pandas dataframe for plotting nested pie/donut charts?
body: <p>This is similar but it is dated and the code doesn't work with the current version of Pandas: <a href="https://stackoverflow.com/questions/33019879/hierarchic-pie-donut-chart-from-pandas-dataframe-using-bokeh-or-matplotlib">Hierarchic pie/donut chart from Pandas DataFrame using bokeh or matplotlib</a></p>
  <p>Here's a common example of what I'm trying to achieve; though it doesn't have to be exact:</p>
  <p><a href="https://i.stack.imgur.com/0YNFbm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0YNFbm.png" alt="enter image description here" /></a></p>
  <p>I'm trying to create a chart that looks like this but with labels. I understand labels at every level will be absurd so I'm looking for a way to say anything under a particular count will be grouped as &quot;Other&quot;:
  <a href="https://matplotlib.org/3.5.1/gallery/pie_and_polar_charts/nested_pie.html" rel="nofollow noreferrer">https://matplotlib.org/3.5.1/gallery/pie_and_polar_charts/nested_pie.html</a></p>
  <p>I have the following table:
  <a href="https://pastebin.com/raw/vC5C355D" rel="nofollow noreferrer">https://pastebin.com/raw/vC5C355D</a></p>
  <pre class="lang-py prettyprint-override"><code>import matplotlib.pyplot as plt
  import pandas as pd
  
  df = pd.read_csv(&quot;https://pastebin.com/raw/vC5C355D&quot;, sep=&quot;\t&quot;, index_col=0)
  </code></pre>
  <p>To be honest, I don't even know where to start. There are 5 different hierarchical levels <code>[class, order, family, genus, species]</code> in that order of hierarchy.</p>
  <p>Do I go through each level and do <code>.value_counts()</code> for each column? If so, how is the hierarchy preserved? I'm not sure how to structure the dataframe to plot this.</p>
  <p><strong>Can someone provide some assistance in how to 1) structure the dataframe so it can be used for hierarchical pie/donut charts; and 2) how to adapt <a href="https://matplotlib.org/3.5.1/gallery/pie_and_polar_charts/nested_pie.html" rel="nofollow noreferrer">the documentation</a> to said dataframe?</strong></p>