Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit several elements in df.columns

For example, the elements of the columns is ['a', 'b', 2006.0, 2005.0, ... ,1995.0]

Now, I hope to change the float to int, so the correct elements of the columns should be ['a', 'b', 2006, 2005, ... , 1995]

Since there are many numbers here, I don't think rename(columns={'old name': 'new name'}) is a good idea. Can anyone tell me how to edit it?

like image 852
Kai Avatar asked Oct 26 '25 05:10

Kai


1 Answers

You can do this:

In [49]: df
Out[49]:
   a  b  2006.0  2005.0
0  1  1       1       1
1  2  2       2       2

In [50]: df.columns.tolist()
Out[50]: ['a', 'b', 2006.0, 2005.0]

In [51]: df.rename(columns=lambda x: int(x) if type(x) == float else x)
Out[51]:
   a  b  2006  2005
0  1  1     1     1
1  2  2     2     2
like image 94
MaxU - stop WAR against UA Avatar answered Oct 27 '25 19:10

MaxU - stop WAR against UA