Creating lists
There are only two occasions when you will have columns of the List
data type. One is when Polars recognizes the List
data type upon reading data. Another is when you create a list
column in your code. Whether it’s splitting a string into a list of strings or combining values from multiple columns, creating lists is the start of your complex analysis involving nested data structures.
In this recipe, we’ll cover how to create lists by splitting strings, grouping by columns, and combining multiple values into lists.
How to do it...
Here’s how to create lists:
- Create lists from strings using
.str.split()
to split strings into lists:df.select( 'tags', pl.col('tags').str.split('|').alias('tags in list') ).head()
The preceding code will return the following output:
Figure 7.2 – The first five rows in the DataFrame...