简体   繁体   中英

Filter DataFrame columns with regex

I count statistics for the dataset, and I want to filter columns that contain specific strings. How I could do it with regex?

Here in volumes_c I filtered some structures, that have Volume in there names

Select_list = ["Amygdala", "Hippocampus", "Lateral-Ventricle", "Pallidum", "Putamen", "Thalamus", "Caudate"]
Side = ["Left", "Right"]
#Selected columns
if(Select_list):
    for s in Side:
        for struct in Select_list:
            volumes_c = group_c.filter(regex="^(?=.*"+s+")(?=.*"+struct+")(?=.*Volume)")

Now i want to filter columns that contain SurfArea in:

冲浪区

Suppose DataFrame data is in the variable df , so the filter will be:

 df.filter(like="SurfArea", axis=1)

Actually, 'axis' arg has a default value 1 and you can omit it, but if you want to filter by rows set it up as 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM