I need to change the type of few columns in a pandas dataframe. Can't do so using iloc I need to change the type of few columns in a pandas dataframe. Can't do so using iloc pandas pandas

I need to change the type of few columns in a pandas dataframe. Can't do so using iloc


I guess it is a bug in 1.0.5. I tested on my 1.0.5. I have the same issue as yours. The .loc also has the same issue, so I guess pandas devs break something in iloc/loc. You need to update to latest pandas or use a workaround. If you need a workaround, using assignment as follows

df1[df1.columns[0:27]] = df1.iloc[:, 0:27].astype('int')

I tested it. Above way overcomes this bug. It will turn first 27 columns to dtype int32


Just don't use iloc. You can just create a loop over the 27 columns and convert them into the data type that you want.

df.info()my_columns = df.columns.to_list()[0:27]for i in my_columns:   df[i] = df[i].astype('int32')df.info()