Skip to content Skip to sidebar Skip to footer

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', '

Solution 1:

You can do this:

In[49]: dfOut[49]:
   ab2006.02005.00111112222In[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]:
   ab200620050111112222

Solution 2:

I think you can use list comprehension:

print (df.columns.tolist())
['a', 'b', 2006.0, 2005.0, 1995.0]

print ([int(col) iftype(col) == floatelse col for col in df.columns])
['a', 'b', 2006, 2005, 1995]

df.columns =  [int(col) iftype(col) == floatelse col for col in df.columns]
['a', 'b', 2006, 2005, 1995]

Post a Comment for "How To Edit Several Elements In Df.columns"