Grow a Treemap with Python and Plotly Express

This text discusses converting a government PDF into a financial planning tool using treemaps, Python, Plotly Express, and tabula-py. It outlines the process of extracting data from a Bureau of Labor Statistics PDF, cleaning it, and creating treemaps to visualize expenditure data for different age brackets. The article emphasizes the utility of treemaps for visualizing and communicating hierarchical data.

 Grow a Treemap with Python and Plotly Express

“`html

Turn a government PDF into a financial planning tool

Photo by Robert Murray on Unsplash

Hierarchical data and Treemaps

Hierarchical data is a data model where items are linked to each other in parent-child relationships, forming a tree structure. Treemaps are diagrams that represent hierarchical data using nested rectangles, with the area of each rectangle corresponding to its numerical value. Treemaps are useful for displaying hierarchical data, highlighting proportions, distinguishing categories, spotting patterns, and bringing a fresh look to data visualization.

Consumer Expenditure Surveys

With the pandemic and subsequent inflationary surge, consumer spending has drawn a lot of attention. To help track income and spending, the Census Bureau uses Consumer Expenditure Surveys to collect information on US consumers’ expenditures, income, and demographic characteristics. The Bureau of Labor Statistics (BLS) compiles these statistics into annual reports published in September.

The Plotly Express and Tabula Libraries

Plotly Express is a higher-level version of the Plotly graphing library, allowing easy production of attractive figures with built-in functionality. Tabula-py is a Python wrapper of tabula-java, enabling table extraction from a PDF.

The Process

Our job will be to convert the BLS “Table 1300” PDF into a pandas DataFrame and then into a treemap. We’ll use tabula-py to read the PDF from the web and convert it to a CSV, then pandas to convert the CSV to a DataFrame. We’ll clean and prepare the DataFrame for plotting and use Plotly Express to create the treemap.

Importing Libraries

We’ll import the necessary libraries and set up Jupyter Notebook to show the maximum rows in a DataFrame and round float values in DataFrames to two decimal places.

Converting the PDF Table into a DataFrame

We’ll use tabula-py to convert the web-based PDF into a CSV file and then use pandas to convert the CSV file into a DataFrame.

Wrangling the Data

We’ll clean the DataFrame and prepare it for plotting by extracting and organizing the necessary data.

Plotting the Treemap

We’ll use Plotly Express to create an interactive treemap for visualizing the average expenditures for different age brackets.

Caveat Emptor!

The values in the BLS table represent nationwide averages, and local variations may occur. Therefore, this data should be used directionally, rather than absolutely, for any financial planning purposes.

Summary

The tabula-py module greatly simplifies the process of converting PDF tables into useful formats like CSV. Likewise, Plotly Express, paired with a pandas DataFrame, makes it easy to generate interactive treemaps. With a treemap, you can easily visualize and communicate relationships in hierarchical data.

Grow a Treemap with Python and Plotly Express

If you want to evolve your company with AI, stay competitive, and use Grow a Treemap with Python and Plotly Express for your advantage. Discover how AI can redefine your way of work. Identify Automation Opportunities, Define KPIs, Select an AI Solution, and Implement Gradually. For AI KPI management advice, connect with us at hello@itinai.com. And for continuous insights into leveraging AI, stay tuned on our Telegram or Twitter.

Spotlight on a Practical AI Solution

Consider the AI Sales Bot from itinai.com/aisalesbot designed to automate customer engagement 24/7 and manage interactions across all customer journey stages.

Discover how AI can redefine your sales processes and customer engagement. Explore solutions at itinai.com.


“`

List of Useful Links:

AI Products for Business or Try Custom Development

AI Sales Bot

Welcome AI Sales Bot, your 24/7 teammate! Engaging customers in natural language across all channels and learning from your materials, it’s a step towards efficient, enriched customer interactions and sales

AI Document Assistant

Unlock insights and drive decisions with our AI Insights Suite. Indexing your documents and data, it provides smart, AI-driven decision support, enhancing your productivity and decision-making.

AI Customer Support

Upgrade your support with our AI Assistant, reducing response times and personalizing interactions by analyzing documents and past engagements. Boost your team and customer satisfaction

AI Scrum Bot

Enhance agile management with our AI Scrum Bot, it helps to organize retrospectives. It answers queries and boosts collaboration and efficiency in your scrum processes.