|
|
大鼻子的枕头 · 购买原始股上市后抛售获利行为的定性| ...· 5 月前 · |
|
|
怕老婆的帽子 · 南开雄安校友会-南开校友网· 5 月前 · |
|
|
年轻有为的鸵鸟 · 开博尔C3双核版固件-开博尔|纯银音频线|光 ...· 5 月前 · |
|
|
发财的山羊 · 总理亲自为报告加入《后汉书》典故:简除烦苛, ...· 5 月前 · |
|
|
要出家的大熊猫 · 法務部-中華民國刑法20250801· 5 月前 · |
我有一个pandas dataframe,它有以下列名:
Result1、Test1、Result2、Test2、Result3、Test3等
我想删除名称中包含单词"Test“的所有列。这些列的数量不是静态的,而是依赖于前一个函数。
我该怎么做呢?
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
使用
DataFrame.select
方法:
In [38]: df = DataFrame({'Test1': randn(10), 'Test2': randn(10), 'awesome': randn(10)})
In [39]: df.select(lambda x: not re.search('Test\d+', x), axis=1)
Out[39]:
awesome
0 1.215
1 1.247
2 0.142
3 0.169
4 0.137
5 -0.971
6 0.736
7 0.214
8 0.111
9 -0.214
你可以使用' filter‘过滤掉你想要的列
import pandas as pd
import numpy as np
data2 = [{'test2': 1, 'result1': 2}, {'test': 5, 'result34': 10, 'c': 20}]
df = pd.DataFrame(data2)
c result1 result34 test test2
0 NaN 2.0 NaN NaN 1.0
1 20.0 NaN 10.0 5.0 NaN
现在过滤
df.filter(like='result',axis=1)
得到..。
result1 result34