Get hands-on experience working with pandas in VS Code by importing real-world financial data and transforming it into a usable DataFrame. This article walks through importing, reading, and verifying a CSV file containing stock and market cap data for major global companies.
Key Insights
- Learn how to import pandas using the common alias pd and read in a CSV file using pandas' read_csv function to create a DataFrame named
companies
. - The dataset includes key financial data such as company names, stock symbols, market capitalizations, stock prices, and countries of origin.
- Noble Desktop recommends actively practicing by pausing between tasks to try coding exercises independently, which helps reinforce pandas concepts and build stronger skills.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
Now that we have gotten our notebook up and running in vs. Code, let's get some pandas practice and some plotting practice, although actually using pandas, not PyPoP. So to import pandas, we're going to write import pandas as pd. That's the very common shortening for pandas.
And we'll execute this. There we go. All right.
Now we have some data for us to work with. It's in this largest companies CSV. And this CSV has a few headings.
And it's all about the stock price and market cap for a number of different companies in different countries. And you can see here's an example, Apple. And it's symbol, it's market cap, it's price in the country.
All right. We're going to get some practice with it now. First, we're going to read it in and make it into a data frame.
And then we'll get some data frame practice. Now, there's a lot of sort of warm up, slash stretching your bounds, slash refresher, slash maybe learning if you haven't done that much pandas work. Now, as we're doing this, make sure to do it on your own.
Every time we're starting a new cell, trying a new task, I highly recommend you pause the video and work on it on your own for a moment. And then come back to the video and see what we did. I think that will be a much better learning process for you because it's one thing to have the code shown to you.
It's another thing to have you type it up and work through the problem on your own. That will lead to much better learning outcomes for you. But I'm not going to keep harping on that.
Just please do that. It will really help you. And I will always pause for a moment before giving you the answer to make sure that you can.
Okay. Let's read in this CSV. We're going to say companies.
Seems like a good name for this data frame. Pandas. And we're not going to make a new CSV, a new data frame.
We're going to read in from and technically we're making a new data frame. We're not making one from scratch. We're getting it from this CSV.
And the path to it is actually very easy. It's in the same folder. And as the note here says, when we're writing things in a script, which we'll be doing most of the time, file paths are relative to where the script is run.
But in a notebook, it's just relative to that particular notebook's location. We put this in the same folder. So it's very easy to say largestcompanies.csv. Mind your spelling.
I almost got the wrong case. And then we'll just take a look at companies. Make sure that we read it right.
Make sure it is what we think it is. We can execute it by pressing this execute up here. And here's a nice little note.
You can also do that by doing control enter or command enter on macOS. I'm wrong about that, actually. It's just control enter.
I'm confusing my shortcuts. Mac or Windows or Linux, control enter is the right shortcut. Glad I checked that.
All right. So here's our data frame. We've read formatting it, querying it, and generally practicing our data frame.