Home > database >  Removing dot symbol from string - Pandas Dataframe
Removing dot symbol from string - Pandas Dataframe

Time:03-15

I'm trying to replace '.' symbol to '':

excel_data_df['serialNumber'] = df2[['Serial number', 'Serial number.1']].agg(''.join, axis=1).replace(to_replace = '.', value = '', regex = True)

My string: "TF013168." Name: serialNumber, dtype: object, number saved as text in the excel.

But as the result I get all the characters removed from the string. Is there any other way to do it?

Thanks in advance.

CodePudding user response:

Escape . by \., because . is special regex character for replace substrings:

excel_data_df['serialNumber'] = df2[['Serial number', 'Serial number.1']].agg(''.join, axis=1).replace(to_replace = '\.', value = '', regex = True)

CodePudding user response:

Use the regex=False option of str.replace to prevent interpretation of . as an "any character" regex. No need to use a regex engine in your case. NB. the regex=True default is planned to be changed to regex=False in the future anyway.

excel_data_df['serialNumber'] = (df2[['Serial number', 'Serial number.1']]
                                 .agg(''.join, axis=1)
                                 .replace(to_replace='.', value='', regex=False)
  • Related