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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With