Search code examples
python-3.xpandasdataframemachine-learningdata-science

How to convert string array data type to float array data type?


I am working on thermal sensor data. The first three data are as follows. Data is in an array of strings. Please find the data link here drive.google.com/file/d/1EfJOFszf7z1aOosc-PbKSsYcEE8ePjLk/…. Thank you so much for helping.

[25.64177131652832, 25.456825256347656, 24.92892074584961, 24.316755294799805, 23.454368591308594, 22.40850067138672, 21.829116821289062, 21.764848709106445, 21.72584342956543, 24.04592514038086, 24.358749389648438, 24.438608169555664, 24.78814697265625, 24.301586151123047, 24.657527923583984, 24.677270889282227, 24.422500610351562, 24.059892654418945, 23.85132598876953]

[23.502470016479492, 23.628114700317383, 23.68063735961914, 23.141122817993164, 22.970962524414062, 22.815067291259766, 22.713998794555664, 22.732934951782227, 22.681840896606445, 23.653491973876953, 23.332386016845703, 23.260208129882812, 23.49359703063965, 23.29375648498535, 23.734455108642578, 23.88347053527832, 23.9250431060791, 23.524120330810547, 23.196813583374023]

[23.735689163208008, 23.837121963500977, 24.044170379638672, 23.415151596069336, 21.520160675048828, 20.2335205078125, 19.441017150878906, 19.177982330322266, 18.871313095092773, 23.82925796508789, 24.192703247070312, 23.958843231201172, 23.816261291503906, 23.37897300720215, 23.469127655029297, 23.600635528564453, 23.589786529541016, 23.144105911254883, 22.776491165161133]

How can I convert this data to float-type data for machine learning models? Thank you.

I tried the following:

df["thermal2"] = pd.to_numeric(df["thermal"],errors="coerce")

0    NaN
1    NaN
2    NaN
     ..
83   NaN
84   NaN

Name: thermal2, Length: 85, dtype: float64

Solution

  • The following code is working for me:

    def to_float(inps: str):
        ret = []
        for inp in inps:
            # Removing square braces
            inp = inp[1:-1]
            # Split using comma
            inp = inp.split(',')
            # Convert each string to float
            ret.append(list(map(float, inp)))   
        return ret