Basic Data Operations in Polars
Stryg for at vise menuen
When you work with large datasets, efficient data manipulation is essential. The polars library is designed for high-performance data operations, making it a popular choice for handling large data in Python. In this chapter, you will learn how to load data, select specific columns, and filter rows using polars. These basic actions form the foundation for more complex data transformations.
The table below summarizes the main functions in polars for performing these basic operations.
1234567import polars as pl # Read data from a CSV file df = pl.read_csv("data/people.csv") # Display the first 5 rows print(df.head())
In this code, you import the polars library and use the pl.read_csv() function to load data from a file named "data/people.csv". The resulting DataFrame is stored in the variable df. By calling df.head(), you can view the first five rows of the DataFrame, which is useful to quickly inspect your data after loading.
123456789import polars as pl # Load the dataset df = pl.read_csv("data/people.csv") # Select the "name" and "age" columns selected = df.select(["name", "age"]) print(selected)
Here, you use the select() method to choose only the "name" and "age" columns from the DataFrame. This creates a new DataFrame called selected containing just those columns. Selecting columns is a common operation when you want to focus on specific parts of your data for further analysis.
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat