Mr. Student.exe
Mr. Student.exe

Reputation: 29

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

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.

Upvotes: 1

Views: 62

Answers (3)

TomYabo
TomYabo

Reputation: 34

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

Upvotes: 2

Dr. Joy Singhal
Dr. Joy Singhal

Reputation: 100

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

Upvotes: 2

Mike R
Mike R

Reputation: 589

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

Upvotes: 1

Related Questions