TDM 10100: Project 4 Question 2 examples

Here are some examples about reading data into Polars data frames. Notice that the notation for Polars looks almost the same as Pandas.

Polars can be used in several different languages. In particular, we can use Polars in both Python and R. We have Polars installed in the seminar kernel for Python (but not for R).

Example 1

We read data into a Polars data frame in the steps below, first by loading Polars:

import polars as pl

and then reading in the Polars data frame. Note that we used pl instead of pd here:

myDF = pl.read_csv("/anvil/projects/tdm/data/icecream/combined/reviews.csv")

Now we can check that we read it in properly:

myDF.head()

and we can even summarize a column to be sure:

myDF['brand'].value_counts()

Example 2

Once we have imported Polars in our current session, we do not need to re-import it. We can just read in more data frames, e.g., like this:

myDF = pl.read_csv("/anvil/projects/tdm/data/flights/subset/airports.csv")

Now we can check that we read it in properly:

myDF.head()

and we can even check which states have the most airports. Please note that the value_counts are NOT sorted by default in Polars, but we can sort the results:

myDF['state'].value_counts().sort('count', descending=True)

As a minor note, Florida and Ohio both have 100 airports. Polars puts Florida first and Pandas puts Ohio first. It is important to be cognizant of ties and cutoffs.