how do i select the columns containing a certain string in their headers in a dataframe using polars in python

main.py
import polars as pl

# Create a dataframe
df = pl.DataFrame(
    {
        "name_abc": ["Alice", "Bob", "Charlie"],
        "age_xyz": [25, 30, 35],
        "city_pqr": ["New York", "Los Angeles", "Chicago"]
    }
)

# Select columns containing the string 'abc' in their headers
selected_cols = [col for col in df.columns if 'abc' in col]

# Subsetting the dataframe using the selected columns
result = df.select(selected_cols)

print(result)
443 chars
19 lines

related categories

gistlibby LogSnag