Chapter 10. Selecting and Creating Columns
Now that you have a solid understanding of how expressions work, let’s look at how to use them. In this chapter, we cover operations that are related to the columns of a DataFrame.1 We’ll focus on selecting existing columns and creating new ones, which are probably the most common operations when working with data.
First, we’ll revisit how to select columns using the df.select() method that we’ve already seen in Chapter 7.
Then we’re going to introduce you to a more flexible way of selecting columns: using so-called column selectors.
Selectors offer various ways to specify columns based on their name, data type, and position.
They can also be combined in different ways.
We’ll continue with how to create new columns and rearrange them.
Finally, we’ll briefly discuss related column operations, such as renaming and dropping columns and combining the columns of two DataFrames.
You’ll be working with a DataFrame about our favorite rebels from the Star Wars universe:
starwars=pl.read_parquet("data/starwars.parquet")rebels=starwars.drop("films").filter(pl.col("name").is_in(["Luke Skywalker","Leia Organa","Han Solo"]))(rebels[:,:6])(rebels[:,6:11])(rebels[:,11:])
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access