从pandas DataFrame中删除名称包含特定字符串的列
发布于 2021-01-29 19:05:05
我有一个带有以下列名称的pandas数据框:
Result1,Test1,Result2,Test2,Result3,Test3等…
我要删除名称包含单词“ Test”的所有列。这样的列数不是静态的,而是取决于先前的功能。
我怎样才能做到这一点?
关注者
0
被浏览
47
1 个回答
-
import pandas as pd import numpy as np array=np.random.random((2,4)) df=pd.DataFrame(array, columns=('Test1', 'toto', 'test2', 'riri')) print df Test1 toto test2 riri 0 0.923249 0.572528 0.845464 0.144891 1 0.020438 0.332540 0.144455 0.741412 cols = [c for c in df.columns if c.lower()[:4] != 'test'] df=df[cols] print df toto riri 0 0.572528 0.144891 1 0.332540 0.741412