0

in Python I have a data frame (df) that contains columns with the following names A_OPEN, A_CLOSE, B_OPEN, B_CLOSE, C_OPEN, C_CLOSE, D_ etc.....

How can I easily select only the columns that contain _CLOSE in their name? A,B,C,D,E,F etc can have any value so I do not want to use the specific column names

In SQL this would be done with the like operator: df[like'%_CLOSE%']

What's the python way?

Goofball
  • 735
  • 2
  • 9
  • 27

1 Answers1

1

You could use a list comprehension, e.g.:

df[[x for x in df.columns if "_CLOSE" in x]]

Example:

df = pd.DataFrame(
   columns = ['_CLOSE_A', '_CLOSE_B', 'C'],
   data = [[2,3,4], [3,4,5]]
)

Then,

>>>print(df[[x for x in df.columns if "_CLOSE" in x]])
   _CLOSE_A  _CLOSE_B
0         2         3
1         3         4
Nelewout
  • 6,281
  • 3
  • 29
  • 39