PagMax
PagMax

Reputation: 8568

How to remove commas from ALL the column in pandas at once

I have a data frame where all the columns are supposed to be numbers. While reading it, some of them were read with commas. I know a single column can be fixed by

df['x']=df['x'].str.replace(',','')

However, this works only for series objects and not for entire data frame. Is there an elegant way to apply it to entire data frame since every single entry in the data frame should be a number.

P.S: To ensure I can str.replace, I have first converted the data frame to str by using

df.astype('str')

So I understand, I will have to convert them all to numeric once the comma is removed.

Upvotes: 33

Views: 98285

Answers (3)

Rakesh Babu
Rakesh Babu

Reputation: 79

In case you want to manipulate just one column:

df.column_name = df.column_name.apply(lambda x : x.replace(',',''))

Upvotes: 1

Azuuu
Azuuu

Reputation: 894

Well, you can simplely do:

df = df.apply(lambda x: x.str.replace(',', ''))

Hope it helps!

Upvotes: 4

jezrael
jezrael

Reputation: 862521

Numeric columns have no ,, so converting to strings is not necessary, only use DataFrame.replace with regex=True for substrings replacement:

df = df.replace(',','', regex=True)

Or:

df.replace(',','', regex=True, inplace=True)

And last convert strings columns to numeric, thank you @anki_91:

c = df.select_dtypes(object).columns
df[c] = df[c].apply(pd.to_numeric,errors='coerce')

Upvotes: 64

Related Questions