how to delete commas in whole DataFrame using pandas or python

Rock Lee :

I'm complete newby to any kind of these programs.

I studied philosophy and economy and trying to learn python for web crawler for my own investment strategy.

I'm from South Korea, so I'm quite nervous to type English here, but I'm trying to be brave! (please, excuse my ugly English)

enter image description here

this is the DataFrame that I've got from the website.

I'm crawling financial datas and as you might see, numbers has commas in it.

their types are object.

what I want to do is to make them integer so I can do some math.(sum, multiplication, etc.)

I searched (including Korean web sites) and I found the way to do using columns name, like this code

cols = ['col1', 'col2', ..., 'colN']

df[cols] = df[cols].replace({'\$': '', ',': ''}, regex=True)

But, what I need is doing it regardless columns' name

I need over 2000 companies' data and columns' names are different depending on company

I'd like to make a code like

"Delete ',' in cols, cols from col#0 to col#end"

Thanks in advance

Manish Chaudhary :

the very first thing you can do is to differentiate data frame by their type and do the processing they needed.

object_list = list(df.select_dtypes(include ="object"))
float_list = list(df.select_dtypes(include ="float64"))
int_list = list(df.select_dtypes(include ="int64"))

then replace whatever you need

df[object_list] = df[object_list].replace(",","")

df[float_list ] = df[float_list ].apply(str) # so that you can replace easily
df[float_list ] = df[float_list ].replace(",","")
df[float_list ] = df[float_list ].apply(float) # now its clean and int

df[int_list ] = df[int_list ].apply(str)
df[int_list ] = df[int_list ].replace(",","")
df[float_list ] = df[float_list ].apply(int)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=320129&siteId=1