Creating and displaying hierarchical trees in Python with pandas

So, I have hierarchical information stored in a pandas DataFrame and I would like to build and render a hierarchical tree based on this information.

For example, a row in my DataFrame has column headings - ['Phylum', 'Class', 'Order', 'Family', 'Genus', 'Kind', 'Subtype']

and I want to create a tree with each row where all "Subspecies" are unique rows and must be leaves in the tree. Can anyone point me to the best method / package, etc ... for this? ideally the output will be a matplotlib object. Thank you in advance!

+3


source to share


1 answer


You can easily get them in a hierarchical index using groupby:

taxons = ['Phylum','Class','Order','Family','Genus','Species','Subspecies']
hierarchical_df = my_dataframe.groupby(taxons).sum() #sum or whatever is most appropiate for your data

      



From there I am also trying to make a meaningful plot showing what the hierarchy is (see Pie / donut hierarchical plot from Pandas DataFrame using bokeh or matplotlib? )

+1


source







All Articles