简体   繁体   English

使用正则表达式过滤 DataFrame 列

[英]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 namesvolumes_c我过滤了一些结构,其中名称中有 Volume

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:现在我想过滤包含SurfArea列:

冲浪区

Suppose DataFrame data is in the variable df , so the filter will be:假设 DataFrame 数据在变量df 中,因此过滤器将是:

 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.实际上,'axis' arg 的默认值是 1,您可以省略它,但是如果您想按行过滤,请将其设置为 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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