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, their data type, and their 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.

Get Python Polars: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.