Search code examples
pythonpython-polars

How to filter empty strings from a list column of Python Polars Dataframe?


I have a python polars dataframe as-

df_pol = pl.DataFrame({'test_names':[['Mallesham','','Bhavik','Jagarini','Jose','Fernando'],
                                    ['','','','ABC','','XYZ']]})

I would like to get a count of elements from each list in test_names field not considering the empty values.

df_pol.with_columns(pl.col('test_names').list.len().alias('tot_names'))

enter image description here

Here it is considering empty strings into count, this is why we can see 6 names in list-2. actually it has only two names.

required output as:

enter image description here


Solution

  • You can use list.eval to run any polars expression on the list's elements. In an list.eval expression, you can pl.element() to refer to the lists element and then apply an expression.

    Next we simply use a filter expression to prune the values we don't need.

    df = pl.DataFrame({
        "test_names":[
            ["Mallesham","","Bhavik","Jagarini","Jose","Fernando"],
            ["","","","ABC","","XYZ"]
        ]
    })
    
    df.with_columns(
        pl.col("test_names").list.eval(pl.element().filter(pl.element() != ""))
    )
    
    shape: (2, 1)
    ┌─────────────────────────────────────┐
    │ test_names                          │
    │ ---                                 │
    │ list[str]                           │
    ╞═════════════════════════════════════╡
    │ ["Mallesham", "Bhavik", ... "Fer... │
    │ ["ABC", "XYZ"]                      │
    └─────────────────────────────────────┘