Reputation: 1493
What is the easist way to retrieve all columns from a DataFrame based on a part of their name, but with different selection patterns? I am too lazy to type the exact column names, and I am looking for a convenient way.
Say, I want all columns that start with the code 'T1*' or 'T2*' - ideally a command like
df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns={'T101', 'T201', 'G101'})
df.filter(like=['T1', 'T2'])
which is not supported, as like=''
only takes 1 string.
Slow workaround I currently use:
col_list = df.columns
target_cols = [e for e in col_list if any(se in e for se in ['T1','T2'])]
df[target_cols]
which gives me the desired output:
>>> df[target_cols]
T201 T101
0 2 3
1 5 6
2 8 9
Is there any faster/more convenient way?
Note this is not about the actual data in the rows/I am not looking for a mask to select rows.
Upvotes: 0
Views: 127
Reputation: 24314
you can use regex
parameter in filter()
method:
df.filter(regex='^T1|^T2')
output:
T101 T201
0 1 3
1 4 6
2 7 9
Upvotes: 2