Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds

Setting Up Polars for Data Analysis

Save for later
  • 420 min read
  • 2024-02-23 12:50:23

article-image

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. 

setting-up-polars-for-data-analysis-img-1 
Figure – Install Jupyter extension in Visual Studio Code 

3. Now back to our file. 

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. 

setting-up-polars-for-data-analysis-img-2 
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. 

setting-up-polars-for-data-analysis-img-3 
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.

Modal Close icon
Modal Close icon

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant