Aspirant
Aspirant

Reputation: 2278

Exclude one column in matching pattern

I have a data frame with multiple columns

 ID|NAME|CITY|AGE|A Bot(S)_S|B Cost_S|C Value(!)_S|D Bot($)_S|E Value(!)_S

I am able to find the columns ending with '_S' using below code

 df.columns[df.columns.str.contains('_S')]

But i need one or more columns to be excluded example : 'D Bot($)_S' while fetching.

EDIT : Correction in column names , they are having space in between.

How can i do it ?

Upvotes: 2

Views: 251

Answers (3)

Toukenize
Toukenize

Reputation: 1420

EDIT

This give you what you need (the regex reads as anything that does not start with D, followed by anything - denoted with .*, and lastly _S):

df.columns[df.columns.str.contains('^[^D].*_S')]

If you want to exclude something else that starts with A instead like Auto Bot($)_S, change the D to A

Upvotes: 1

Shubham Sharma
Shubham Sharma

Reputation: 71689

Try this:

UPDATE:

df.columns[df.columns.str.contains(r"^[^D].*_S")]

Upvotes: 1

Chris
Chris

Reputation: 29742

Use pandas.DataFrame.filter:

df.filter(regex="[^D]_S")

Upvotes: 1

Related Questions