Grouping and Charting Market Cap by Country

Group companies by country, sum their market caps, sort and select the top five, then plot a pie chart of their market cap distribution using matplotlib.

Learn how to group and analyze financial data using Python to find the countries with the highest total market capitalization. This article shows how to visualize the top five countries by market cap through pie chart creation using pandas and matplotlib.

Key Insights

  • Demonstrates how to use the groupby function in pandas to aggregate market capitalization data by country, identifying the total market cap for each unique country in the dataset.
  • Explains how to sort the aggregated data in descending order and extract the top five countries based on market cap for further analysis and visualization.
  • Shows how to create a pie chart using pandas' plot method with matplotlib to visually represent the top five countries by market cap, including configuration for labels, percentages, and formatting.

This lesson is a preview from our Data Analytics Certificate Online (includes software). Enroll in this course for detailed lessons, live instructor support, and project-based training.

Now we're going to do a group by. If you haven't done a group by before, we're not going to teach you group by exactly here, but I'll explain what we're doing as we're doing because they're a little more complicated. So we're going to try to get companies group by country, basically meaning we want to have countries.

We want to have one column as countries and the second one is the sum of all the market caps for all companies that belong to that country. Let me write that down a little better. Let me sum it up.

We want unique countries. Our data frame has a country, each row has a country that the company is based out of. Unique countries with the total market cap for all companies based out of that country.

All right, here's how we're going to do that. You know what? I'll let you do that. I forgot to let you do that.

Give it a shot. It's definitely a little bit more intricate. Give yourself time if you need it.

How'd you do? Let's take a look. I'm going to call mine countries. We'll do a group by.

With group by, we say what field, what column we want to group them by. Then we say what columns, in this case it's just market cap, we want them to sum up. Then more specifically, we are summing them.

There are other things we can do with a group by, but that's a pretty standard thing to do there. I want all companies grouped by their country. Looking at their market caps, sum up all the market caps.

Let's take a look at that. All companies based out of Argentina add up to $50 billion in market cap. Australia, $1.3 trillion.

You go down to the bottom. Here's a good old US of A with $46 trillion. Okay.

Now that we have that, our goal here is ultimately to chart the top five countries by market cap. First, we need to get the top five companies by market cap. These are not sorted.

The easiest way to get the top five by market cap would be to sort them. We'll do that. We'll say top five equals countries, this thing we just made, but sorted by the value for market cap.

We'll also say for the sorting, add another argument, ascending equals false. Otherwise, it'll be from lowest market cap up to highest. That gives us all the countries, all 68 rows of them, but we want only the top five.

This gives us a big data frame of all of them. Well, let's grab just a slice of that data frame. We say from zero to five.

Zero is understood. Let's look at that top five and see if we did that right. Looks good.

Okay. Our next step is to make a pie chart. Now, we could use pie plot, and I think it's a great thing to do.

Of course, we're going to be using dash, so we're going to keep this simple, but we're going to show how we can do this with pandas. We can look at that top five, and it has a dot plot method on it, which actually uses pie plot under the hood, and barely under the hood. We're going to use some pie plot.

Oh, I forgot we do need to import it in order to do that. I will import it in a moment, but there's going to be a lot of arguments we give to the dot pie method, so I'm going to make this vertical, and we're going to say the y values is market cap column. Don't give me a legend.

Put the percentages in there, and this is kind of a complicated little language here, sub language, which you could absolutely learn, but which we won't go into, and we'll make it an eight by eight figure. Eight by eight is a. Now, I want to say PLT dot show. I'm going to give it a title, but as you can see from this error, I neglected to actually import pie plot, so let's go back up the very top, import pandas as PD.

Let's also import matplotlib dot pie plot as PLT, and don't forget to run that, which I almost did, and it looks like I messed it up. That's what I get for writing that code from memory. Matplotlib dot pie plot.

Matplotlib. Interesting. Oh, we haven't installed matplotlib in our dvenv.

That's what's going on here. I'm going to open up my terminal. I'm going to make sure I'm in dvenv, and I'm going to say conda install matplotlib.

That'll take just a moment, and once that's done, I'll let that go for a moment, and we'll finish what we were writing. All right. We're going to do PLT dot shovel while we're at it.

Let's also do a title, top five countries, which I can definitely spell by market cap, countries. Nailed it. And let's take off the Y label by passing in an empty string.

All right. Let me switch back to the terminal. How are we doing? Taking a minute.

So you know what? I'm going to pause the video now, and I'll come back, and we'll take a look at this beautiful pie chart.

How to Learn Python

Build practical programming skills with hands-on Python training. Python is a widely used, versatile programming language applied across data analysis, automation, artificial intelligence, and software development.

Yelp Facebook LinkedIn YouTube Twitter Instagram