Python 3.x 基于索引名字符串操作删除列

Python 3.x 基于索引名字符串操作删除列,python-3.x,string,pandas,Python 3.x,String,Pandas,我有一个包含很多列的大型数据框,希望根据对列名的字符串操作删除一些数据框 考虑以下示例: df_tmp = pd.DataFrame(data=[(1,2,3, "foo"), ("bar", 4,5,6), (7,"baz", 8,9)], columns=["test", "anothertest", "egg", "spam"]) 现在,我想删除列名包含测试的所有列;我试图调整给出的答案(对列内容进行字符串操作)和(对名称进行寻址),但没有效果

我有一个包含很多列的大型数据框,希望根据对列名的字符串操作删除一些数据框

考虑以下示例:

df_tmp = pd.DataFrame(data=[(1,2,3, "foo"), ("bar", 4,5,6), (7,"baz", 8,9)],
                     columns=["test", "anothertest", "egg", "spam"]) 
现在,我想删除列名包含
测试
的所有列;我试图调整给出的答案(对列内容进行字符串操作)和(对名称进行寻址),但没有效果

df_tmp = df_tmp[~df_tmp.index.str.contains("test")]
# AttributeError: Can only use .str accessor with string values!

df_tmp[~df_tmp.name.str.contains("test")]
# AttributeError: 'DataFrame' object has no attribute 'name'
有人能给我指出正确的方向吗?
提前多谢。:)

最好使用
df.filter()

>>> df_tmp
  test anothertest  egg spam
0    1           2    3  foo
1  bar           4    5    6
2    7         baz    8    9
结果:

1-

2-

3-

4-

正则表达式解释
Nice

使用:
df_tmp.loc[:,~df_tmp.columns.str.contains(“test”)]
您的代码不起作用,因为您检查的是索引而不是列,返回
True
列的正确方法是通过
df.loc[]
>>> df_tmp.loc[:,~df_tmp.columns.str.contains("test")]
   egg spam
0    3  foo
1    5    6
2    8    9
>>> df_tmp.drop(df_tmp.filter(like='test').columns, axis=1)
   egg spam
0    3  foo
1    5    6
2    8    9
>>> df_tmp.drop(df_tmp.filter(regex='test').columns, axis=1)
   egg spam
0    3  foo
1    5    6
2    8    9
>>> df_tmp.filter(regex='^((?!test).)*$')
   egg spam
0    3  foo
1    5    6
2    8    9
'^((?!test).)*$'

^         #Start matching from the beginning of the string.    
(?!test)  #This position must not be followed by the string "test".
.         #Matches any character except line breaks (it will include those in single-line mode).
$         #Match all the way until the end of the string.