0

I have a list of column names from a data frame:

['s_0','s_1','s_2','s_0_m','s_1_m','s_2_m']

I need to make a comprehension list and select only the names without m at the end (s_0, s_1...) or only the names with m at the end (s_0_m, s_1_m etc).

So I thought it would be something like

[col for cols in df.columns if re.search(regex,col)]

But I can't figure out the regex expression, please help me!

9879ypxkj
  • 387
  • 5
  • 15
  • 1
    You could use `'[^m]$'` (if you don't care about matching empty string). Or you could just do `if not col.endswith('m')`. – alani Jun 14 '20 at 10:34
  • If the `_m` part is always at the end, I would use the `endswith` string method: `with_m = [x for x in col_list if x.endswith('_m') == True]` and `no_m = [x for x in col_list if x.endswith('_m') == False]` – t.novaes Jun 14 '20 at 10:44

1 Answers1

3

I'm not entirely sure why you would use regex when you can do this in pure Python very quickly.

# Initialization
col = ['s_0','s_1','s_2','s_0_m','s_1_m','s_2_m']

# for i in col, return if the last letter (i[-1]) is not equal (!=) to "m"
print([i for i in col if i[-1] != "m"])

# ['s_0', 's_1', 's_2']
alani
  • 12,573
  • 2
  • 13
  • 23
Xiddoc
  • 3,369
  • 3
  • 11
  • 37