pandas: to_numeric for multiple columns

后端 未结 5 1797
刺人心
刺人心 2020-11-27 03:47

I\'m working with the following df:

c.sort_values(\'2005\', ascending=False).head(3)
      GeoName ComponentName     IndustryId IndustryClassification Descri         


        
5条回答
  •  死守一世寂寞
    2020-11-27 04:37

    If you are looking for a range of columns, you can try this:

    df.iloc[7:] = df.iloc[7:].astype(float)
    

    The examples above will convert type to be float, for all the columns begin with the 7th to the end. You of course can use different type or different range.

    I think this is useful when you have a big range of columns to convert and a lot of rows. It doesn't make you go over each row by yourself - I believe numpy do it more efficiently.

    This is useful only if you know that all the required columns contain numbers only - it will not change "bad values" (like string) to be NaN for you.

提交回复
热议问题