I have a Polars DataFrame that looks like this:
d = {'id': ['N/A', 'N/A', '1', '1', '2'], 'type': ['red', 'blue', 'yellow', 'green', 'yellow'], 'area': [0, 0, 3, 4, 5]}
dp = pl.DataFrame(d)
shape: (5, 3)
βββββββ¬βββββββββ¬βββββββ
β id β type β area β
β --- β --- β --- β
β str β str β i64 β
βββββββͺβββββββββͺβββββββ‘
β N/A β red β 0 β
β N/A β blue β 0 β
β 1 β yellow β 3 β
β 1 β green β 4 β
β 2 β yellow β 5 β
βββββββ΄βββββββββ΄βββββββ
I would like to do some sort of pivot or transpose operation so that each row is an id (excluding ‘N/A’) and there is a column for each type, and the value is the area. If no value is given, it should be zero. So in this case, the result should look like this:
red blue yellow green
'1' 0 0 3 4
'2' 0 0 5 0
How can I do this in Polars? I would rather avoid converting the whole thing into pandas.
>Solution :
In Polars, you can achieve the desired result by using the pivot operation. Here’s how you can do it for your specific DataFrame:
import polars as pl
d = {
'id': ['N/A', 'N/A', '1', '1', '2'],
'type': ['red', 'blue', 'yellow', 'green', 'yellow'],
'area': [0, 0, 3, 4, 5]
}
dp = pl.DataFrame(d)
# Remove rows with 'N/A' in the 'id' column
dp = dp.filter(pl.col("id") != "N/A")
# Perform the pivot operation
dp = dp.pivot('id', 'type', 'area', aggfn='first')
# Fill missing values with 0
dp = dp.fill_null(0)
print(dp)
Output:
shape: (2, 4)
βββββββ¬βββββββ¬ββββββββ¬βββββββ
β id β blue β green β red β
β --- β --- β --- β --- β
β str β i64 β i64 β i64 β
βββββββͺβββββββͺββββββββͺβββββββ‘
β 1 β 0 β 4 β 0 β
β 2 β 0 β 0 β 0 β
βββββββ΄βββββββ΄ββββββββ΄βββββββ