Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our
Figure – Creating a new file in Visual Studio Code
Files with extension .ipynb are Jupyter Notebook files, which are great for data analysis.
To work with these files you need to install the Jupyter extension on VS Code. You can do that by clicking on ‘Extensions’ on the left bar, searching for Jupyter, installing it, and activating it.
Figure – Install Jupyter extension in Visual Studio Code
3. Now back to our file.
Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at €18.99/month. Cancel anytime
The first thing to ensure is that we are using Python from our virtual environment. Click on Select Kernel at the top right, then click on the Python that starts with env/: that will be the Python for our virtual environment.
Avoid the paths starting with /usr and /bin as those are the system Python instead of our virtual environment.
Figure – Select the Python interpreter in Visual Studio code
Now, we're ready for Polars.
4. Type import polars as pl in the first cell and press Shift + Enter to run it.
5. Create a dataframe in the next cell by typing:
df = pl.DataFrame({
'a': ['Hello', 'World!']
})
6. Press Shift + Enter to run the cell.
This creates a dataframe called df with one column named 'a' and two rows: 'Hello' and 'World!'
To see the dataframe, type df in the next cell and run it.
Figure – Visual Studio code with first Polars dataframe
We created our first Polars dataframe.
Using Polars on the cloud with Google Colab
Instead of installing Polars on your computer, you can also use it in the cloud. One popular cloud service for running code is Google Colab. This way, you don't need to install anything on your machine.
To access Google Colab, visit https://colab.research.google.com/ in your web browser. Click on "New Notebook," and you'll see a page that looks similar to VS Code.
Now, let's create the same Polars dataframe example in Google Colab:
1. In the first cell, type the following command to ensure we have the latest version of Polars:
%pip install polars --upgrade
2. Next, enter this code to import Polars and create a dataframe:
import polars as pl
df = pl.DataFrame({
'a': ['Hello', 'World !']
})
Finally, display the dataframe by typing:
df
And that's it! You now have your first Polars dataframe in Google Colab.
Luca Zanna is a Data Engineer and Data Analyst with over 15 years of experience. He started his career as a financial data analyst after a Master's in Management and passing the Certified Public Accountant (CPA) exam. Luca spent a decade working on financial analysis systems at L’Oréal: developing the systems and training financial analysts across Europe and Asia.
Currently, Luca helps companies with building data infrastructure to better leverage their data. Luca is also a corporate teacher for topics such as data analysis, SQL, Python, and cloud data engineering.