Home > Enterprise >  Replace part of string in columns names
Replace part of string in columns names

Time:12-09

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

CodePudding user response:

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