11

I have a dataframe with a lot of columns using the suffix '_o'. Is there a way to drop all the columns that has '_o' in the end of its label?

In this post I've seen a way to drop the columns that start with something using the filter function. But how to drop the ones that end with something?

3 Answers 3

21

Pandonic

df = df.loc[:, ~df.columns.str.endswith('_o')]

df = df[df.columns[~df.columns.str.endswith('_o')]]

List comprehensions

df = df[[x for x in df if not x.endswith('_o')]]

df = df.drop([x for x in df if x.endswith('_o')], 1)
Sign up to request clarification or add additional context in comments.

2 Comments

List comprehensions are not correct, it should be df.columns not df. E.g., [x for x in df.columns...]
@Yasmin, No, it's fine.. for x in df loops through the column names of a dataframe.. test it yourself.
2

To use df.filter() properly here you could use it with a lookbehind:

>>> df = pd.DataFrame({'a': [1, 2], 'a_o': [2, 3], 'o_b': [4, 5]})

>>> df.filter(regex=r'.*(?<!_o)$')
   a  o_b
0  1    4
1  2    5

Comments

0

This can be done by re-assigning the dataframe with only the needed columns

df = df.iloc[:, [not o.endswith('_o') for o in df.columns]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.