Reputation: 941
I have a df with the following column names:
Name, Id, xyz,..., ijk, 1, 2, 3, val1, val2,...
I want to drop all the columns whose name doesn't contain number and only keep Name
and Id
. Is there an efficient way to do this?
The result would be: Name, Id, 1, 2, 3, val1, val2,...
Thanks!
Upvotes: 2
Views: 90
Reputation: 7994
With str.match
based on MaxU's answer.
new_columns = df.columns[df.columns.astype(str).str.match("(.*\d.*)|Id|Name")]
df[new_columns]
Upvotes: 3
Reputation: 210832
IIUC:
df = df.loc[:, df.columns.astype(str).str.contains('\d') | df.columns.isin(['Name','Id'])]
Demo:
In [224]: df
Out[224]:
Name Id xyz ijk 1 2 3 val1 val2
0 1 2 3 4 5 6 7 8 9
In [225]: df = df.loc[:, df.columns.astype(str).str.contains('\d') | df.columns.isin(['Name','Id'])]
In [226]: df
Out[226]:
Name Id 1 2 3 val1 val2
0 1 2 5 6 7 8 9
Upvotes: 4