I have the following dataframe that contains columns like:
JavaScript
x
7
1
df
2
3
a b c d a_main b_main c_main d_main
4
row
5
row2
6
row3
7
I would like the column names that contain _main
sent to a list. such as:
JavaScript
1
2
1
collist = ['a_main' , 'b_main' , 'c_main' , 'd_main']
2
How best could I do that? Thanks very much!
Advertisement
Answer
try via filter()
method:
JavaScript
1
2
1
collist=df.filter(like='_main').columns.tolist()
2
OR
Via boolean masking:
JavaScript
1
2
1
collist=df.columns[df.columns.str.endswith('_main')].tolist()
2