简体   繁体   中英

Replace column names with quotations with no quotations

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM