Sharpen Your Pandas Skills with Practical Data Exercises

Practice pandas by cleaning and analyzing a company dataset through scaling values, dropping rows and columns, filtering by conditions, and retrieving specific data points.

Sharpen your pandas skills with hands-on exercises that fine-tune essential data manipulation techniques. From scaling massive numbers into readable formats to filtering and refining datasets, this article walks through practical examples using real-world company data.

Key Insights

  • Transforming large numeric values, such as converting market capitalization figures in the trillions into human-readable formats, requires element-wise division and rounding operations within a pandas Series.
  • Data cleaning tasks demonstrated include dropping irrelevant columns (e.g., "Rank") using both the drop() method with in-place modification and removing rows with invalid values like a stock price of zero using conditional filtering or index-based deletion.
  • Analytical tasks such as identifying the highest stock price, calculating the average price, counting companies under a certain price threshold, and retrieving specific values based on a symbol (e.g., XOM for Exxon) reinforce practical querying techniques taught in Noble Desktop’s pandas training.

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.

This is a lesson preview only. For the full lesson, purchase the course here.

We're going to do some pandas exercises now. Some of which will produce some pretty interesting results, but most of the point is to make sure that your pandas is your pandas skills are sharp. Your muscles are loose.

You're ready to take on some more advanced data visualization. Alright, so we've read in the CSV. We now have our, our data frame here.

Let's do some work with it. We have seven tasks to do. Number one is to put the market cap into a more manageable scale.

If you look at the market cap, this is a really tough to read. I don't even know how big that number is. I mean, I know because I've solved this problem before, but it's a, it's, it's very challenging to even know what that number represents.

How big is that number? Let's put it into a more human readable scale by rounding it down. See if we can get it to one, maybe one number and then a decimal point in a couple other numbers past the decimal point. Up to you exactly what that means.

Take a moment and see if you can solve that problem. Did you get it? Let's give it a shot. I think if we'd say companies market cap divided by.

It turns out these are actually in trillions. One underscore 000 underscore 000 underscore 000 underscore 000 Yep, that's correct. Let's see what that is.

If we take a panda series, remember, like a column and divide it, it will instead not really divide the column, but divide all the elements in the column. Yep, that looks like it's in the right scale. Now, let's round it because right now it's 2.801388. That's too many.

That's too many digits. So, I'm going to run this all through the round command when you zoom out a little bit so that's all one line. There we go.

And rounds requires a second argument, which is how many spaces past the decimal point around two. And there we go. That looks pretty good.

Now that we have a series, a column, that's in the right format we can change companies at the column market cap to be that amount companies market cap equals this series that we just made. All right, let's run that. Oh, and let's take a look at companies I'm actually make a new code block for that.

I don't want to run this again. You can alternately have put companies evaluated companies and then done run all which will start it start the whole thing over. But there we go market cap is now in a sort of human readable format.

Oh well, I hope you were successful doing that on your own, and see how you do on this one. Let's drop the rank column. Don't forget the capital R rank.

Let's drop that one. It's pretty useless information. So drop the rank column.

Let's see how you do. There's a couple of ways to solve this. How'd you do on that.

Let's take a look at, at least I mean there's probably more solutions than this, but this is a pretty good one. We could say, companies.drop columns equals rank. And if we do that, we'll get back a version of the data frame without the rank in it.

However, we didn't actually change companies. If we look, it still has rank. We need to do is either say companies equals this new data frame, or do in place equals true.

And then we can look at companies. Great. No rank.

Now there are some other ways to do this. I'm going to comment out these lines. And I'm going to do run all to reset everything.

And another way to do this, I don't quite like it as much, but it's a perfectly fine way to do it. We have to say if we just do rank instead of columns equals rank is to tell it what axis to look for rank in. And the axis is one for columns.

And then we can also do in place equals true. Oh, and let me look at companies. And run all.

And there we go. Either way is fine, but I prefer that one. But up to you.

All right, task number three. Drop all rows with a price of zero, getting a little, a little more complicated. There's several different ways to do this one.

All right, I'll give you a moment. I'd like to, you know, you could you could pause the video. I don't have to give you that much of a moment.

How'd you do? Let's take a look at one possible solution. Maybe a couple possible solutions. We could have filter.

This is probably the way I prefer to do this. Filter by the condition that companies at price in USD and make sure you're spelling this right with the capital USD with the parentheses of the space is not equal to zero. And then we can look at companies.

And, oh, I was looking at the market cap, I was like, oh, we didn't, it didn't work. But we have market cap zero but no price zero anymore. If we scroll up, we see some with price zero.

In earlier versions. Some other ways we could do this we could use, we could use dot drop. I want to run all.

Get it back. And then I'll do one more possible solution, we can say indexes to drop equals companies, where the condition is companies at price in USD equals zero, that things dot index. That'll give us a group of indexes.

And then we can say, companies dot drop those indexes to drop. And in place equals true. So that's an alternate way to do it.

And yep, we lost those zero dollar ones. I guess if I want to do it too I just I think that the earlier way it's just a little clearer. Okay.

Let's move on to the next task. Get the company with the highest price. All right, I'll give you a moment to do that.

How'd you do. I hope well. Good to get your practice in with this kind of thing.

I'm going to make a variable with this is not technically needed but I think it'll make it a little clearer, clearer code companies, and price that column dot max, that'll give us the highest value in that column. And then we could say, I want companies where the pricing USD column equals that max price. What do we get.

Berkshire Hathaway, and that's correct. How about the average price. This is a very high price what is that 474,000 per share.

That's too much, that's too much for sure. I'm not buying one of those shares. You feel free though.

Now let's get the average price which is going to be far lower than that. I'll give you a moment, see if you can get the average price for a row in our data frame. Okay.

How'd you do. This one's not complicated doesn't mean it's easy. If you don't know how to do it.

Average price equals. We get the column price in USD, and get it's dot mean. Ultimately, you could consider average the median average, also a good average, but typically when we say average we do mean mean.

And now let's take a look at the average price. Yep, that's the correct amount. Now, two more tasks.

How many companies have a price under 100. This is getting a little challenging. Let's see how you do with it.

The tough thing here is not just getting the ones that have the lowest price but their count. All right, I'll give you a moment. How'd you do.

There's a couple different ways to solve this. Companies, they all they both have this in common. Give me the companies where their price in USD is less than 100.

And then from there there's a couple different ways we can get it. We could say give me the counts for that. Now counts is kind of a weird method.

We run that, we get a series where it's the count, but each one has a column name. There are 5,115 with a name, 5,115 with a symbol, but the symbol value isn't really 5,115, that's just the count value. It's kind of funky how it gives you that.

But we could say I want the first value there. I look zero. And that gives us the number 5115.

Alternately, a way I think is slightly easier to read. We can look at the dot shape of it. And that gives us a there's 5,115 rows, five columns of that data frame that's that data frame that's only the ones less than 100.

So then, if we want. This is a tuple if we want the first value in it. We can say, zero.

And there's the number. However you'd like to do it. Okay.

Last task. Let's get really specific. Find me the price for the company with a symbol XOM, which is Exxon.

I'll give you a second. All right, here it is. Companies.

Where and it's kind of strange to filter when you when you're just looking for one thing. There's only one should only be one value with this, that this is true of symbol is XOM. If we look at that, it's this.

We want specifically the price for it. So we could say, give me the price column price. I'm doing this wrong.

Maybe I actually might be doing this totally right I think I'm doing this totally right, and I'm just getting slowed down by the fact that there's no auto complete on this. I don't think it quite knows what it is I'm doing here. But there we go.

The value is 82.39. Okay. And technically this isn't quite the format we want. This isn't actually just a number.

It's a row. Still, what we want is the values from it. And the first value in it.

And there we go. Now we have the actual number 8.39. Okay, well I hope that was helpful. We're going to be working with, you know, these kinds of things filtering and searching, manipulating columns, all of that, as we go.

Next up, we're going to practice a group by some data cleanup, and we'll make a 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