Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In a dataframe with around 40+ columns I am trying to change dtype for first 27 columns from float to int by using iloc:

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

However, it's not working. I'm not getting any error, but dtype is not changing as well. It still remains float.

Now the strangest part:

If I first change dtype for only 1st column (like below):

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

and then run the earlier line of code:

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

It works as required. Any help to understand this and solution to same will be grateful.

Thanks!

like image 765
Rishik Avatar asked Sep 21 '20 08:09

Rishik


People also ask

How do you change specific columns in pandas?

Use the pandas DataFrame. rename() function to modify specific column names. Set the DataFrame columns attribute to your new list of column names.

How do I select multiple columns in ILOC?

To select multiple columns, you can pass a list of column names to the indexing operator. Alternatively, you can assign all your columns to a list variable and pass that variable to the indexing operator.

Can a pandas column have different data types?

Pandas uses other names for data types than Python, for example: object for textual data. A column in a DataFrame can only have one data type.


1 Answers

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

like image 69
Andy L. Avatar answered Oct 04 '22 14:10

Andy L.