3

I have the following pandas DataFrame in python:

attr_x header example_x other
3232 322 abv ideo
342 123213 ffee iie
232 873213 ffue iie
333 4534 ffpo iieu

I want to remove the suffixes '_x' from all columns containing it. The original DataFrame is much longer. Example result:

attr header example other
3232 322 abv ideo
342 123213 ffee iie
232 873213 ffue iie
333 4534 ffpo iieu

1 Answer 1

4

Use str.removesuffix:

df.columns = df.columns.str.removesuffix("_x")

Or replace:

df.columns = df.columns.str.replace(r'_x$', '')
Sign up to request clarification or add additional context in comments.

1 Comment

This for python 3.10 I believe

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.