3

Hi I am trying to delete all hyphens from a csv data table. Where all values are XYZ-001, XYZ-002, XYZ-003, etc AND I want them to look like XYZ001, XYZ002, XYZ003. I cannot seem to figure out how to use the .replace() feature. Any other ideas??

import pandas as pd

dfz = pd.read_csv('control.csv', index_col=0, parse_dates=True)
df1 = pd.DataFrame(dfz[0])
print(dfz['Unnamed: 1'])
dfz['Unnamed: 1'].replace('-','')
Ed_
  • 69
  • 8

2 Answers2

2

you can also use:

df.replace(r'[\W]','',inplace=True,regex=True)

if you dont want to use inplace (you can go through this for pros and cons of inplace) you could assign it back like :

df = df.replace(r'[\W]','',regex=True)

\W finds any character that is not a letter, numeric digit, or the underscore character.

Use this only if you have unknown special characters in the dataframe.

anky
  • 74,114
  • 11
  • 41
  • 70
1
dfz['Unnamed: 1'] = dfz['Unnamed: 1'].str.replace('-','')

Remember to assign it back. Documentation here

ycx
  • 3,155
  • 3
  • 14
  • 26