Converting strings to floats in a DataFrame

前端 未结 6 853
无人及你
无人及你 2020-11-27 12:30

How to covert a DataFrame column containing strings and NaN values to floats. And there is another column whose values are strings and floats; how to convert th

6条回答
  •  情书的邮戳
    2020-11-27 12:42

    Here is an example

                                GHI             Temp  Power Day_Type
    2016-03-15 06:00:00 -7.99999952505459e-7    18.3    0   NaN
    2016-03-15 06:01:00 -7.99999952505459e-7    18.2    0   NaN
    2016-03-15 06:02:00 -7.99999952505459e-7    18.3    0   NaN
    2016-03-15 06:03:00 -7.99999952505459e-7    18.3    0   NaN
    2016-03-15 06:04:00 -7.99999952505459e-7    18.3    0   NaN
    

    but if this is all string values...as was in my case... Convert the desired columns to floats:

    df_inv_29['GHI'] = df_inv_29.GHI.astype(float)
    df_inv_29['Temp'] = df_inv_29.Temp.astype(float)
    df_inv_29['Power'] = df_inv_29.Power.astype(float)
    

    Your dataframe will now have float values :-)

提交回复
热议问题