Tmiskiewicz
Tmiskiewicz

Reputation: 403

Replace part of string in columns names

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

Upvotes: 2

Views: 836

Answers (1)

mozway
mozway

Reputation: 261000

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']

Upvotes: 2

Related Questions