0

I am trying to replace my column names that have quotations and simply remove the quotations but when I try this:

for x in df.columns:
    x = x.replace('"', '')
    print(x)

Nothing happens and the quotations are still there.

Snorrlaxxx
  • 168
  • 1
  • 3
  • 18
  • That's replacing the text in the loop variable without actually modifying the columns. Try using a list comprehension and assign back to df.columns. – cs95 May 14 '20 at 22:54
  • @cs95 If you have an answer please provide one, thanks. – Snorrlaxxx May 14 '20 at 22:55
  • Sure, [take a look](https://stackoverflow.com/a/61815404/4909087), this is arguably better than using a list comprehension, but that's just me. – cs95 May 17 '20 at 01:13

3 Answers3

3

I would do something like this

cols = [column_name.replace('"','') for column_name in df.columns] 
df.columns = cols

CODE

import pandas as pd
df=pd.DataFrame({"a":[1,2],'"b"':[3,4]})
print('BEFORE')
print(df)
cols = [column_name.replace('"','') for column_name in df.columns] 
df.columns = cols
print('AFTER')
print(df)

OUTPUT

BEFORE
   a  "b"
0  1    3
1  2    4
AFTER
   a  b
0  1  3
1  2  4
0

you can remove it by writing the following code:

col=[]
for x in df.columns:
    x = x.replace('"', '')
    col.append(x)

df.columns=col

To know more about column renaming: Check this Renaming columns in pandas

0

One canonical solution to this problem is using pandas str.replace on the header directly (this is "vectorized"):

df = pd.DataFrame({"a": [1, 2], '"b"': [3, 4]})
df.columns = df.columns.str.replace('"', '')

df

   a  b
0  1  3
1  2  4
cs95
  • 379,657
  • 97
  • 704
  • 746