Reading and writing CSV files
Comma-separated values (CSV) is one of the most commonly used file formats for storing data. The structure or the way in which you read a CSV file may be familiar to you if you have worked with another DataFrame library such as pandas.
In this recipe, we’ll examine how to read and write a CSV file in Polars with some parameters. We’ll also look at how we can do the same in a LazyFrame.
How to do it...
Here are the steps and examples for how to read and write CSV files in Polars:
- Read the
customer_shopping_data.csv
dataset into a DataFrame:df = pl.read_csv('../data/customer_shopping_data.csv') df.head()
The preceding code will return the following output:
Figure 2.1 – The first five rows of the customer shopping dataset
- If the CSV file doesn’t have a header, Polars would treat the first row as the header:
df = pl.read_csv('../data/customer_shopping_data_no_header...