Skip to content
Advertisement

Remove commas from all columns except one

df
               
   date           price      vol    
0 2010-01-04  34,57282657    2,600,000
1 2010-01-04  123,900        2,600,000
2 2010-01-04  353,6789738    2,600,000

Is there a way to remove commas from all columns except 1 or 2 (here, just date) in general code? (I have 20 columns in reality.)

Expected output:

   date           price      vol    
0 2010-01-04  3457282657    2600000
1 2010-01-04  123900        2600000
2 2010-01-04  3536789738    2600000

Advertisement

Answer

Use DataFrame.replace on columns of dataframe excluding the columns from exclude list:

exclude = ['date']

c = df.columns.difference(exclude)
df[c] = df[c].replace(',', '', regex=True)

Result:

         date       price      vol
0  2010-01-04  3457282657  2600000
1  2010-01-04      123900  2600000
2  2010-01-04  3536789738  2600000
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement