How would I remove specific characters from only one column in a CSV using python?

Question:

I need to have my column be only integers, but unfortunately the CSV I need to use has the letters T and Z interspersed.

For example:

2022-09-24T00:30:49Z

How would I go about removing these letters for only this one column? I have tried looking elsewhere, but I can’t find anything specifically relating to this.

Asked By: Mr. Student.exe

||

Answers:

This question should at least get you started in the right direction: Remove cell in a CSV file if a certain value is there

Answered By: Mike R
def custom_func(x):
  if isinstance(x,str):
    return x.translate(None,'TZ')
  return x
df.col = df.col.apply(custom_func)

You can use applymap method to apply it to each individual cell if you may

Answered By: Joy Singhal

df["column"]= df["column"].replace(r"T|Z", "", regex = True)

Answered By: TomYabo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.