2

I have a issue with changing columns names. There is a part that I have to remove. Tables looks like below

Column.name_1  Column.name_2  Column.*.name_3  Column_name_4  Column_*_name_5
    

I wrote a line of code that changes dot and strix into underscore:

df_check.columns = df_check.columns.str.replace('.*.', '_')

But I get

Column_name_1  Column_name_2  Column___name_3  Column_name_4  Column___name_5

And I need below result with only one uderscore.

Column_name_1  Column_name_2  Column_name_3  Column_name_4  Column_name_5

Can you help me with this? Regards

1 Answer 1

2

You could use:

df_check.columns = df_check.columns.str.replace(r'[.*_]+', '_', regex=True)

Output names:

['Column_name_1', 'Column_name_2', 'Column_name_3', 'Column_name_4', 'Column_name_5']
Sign up to request clarification or add additional context in comments.

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.