Change column type in pandas

前端 未结 9 1230
萌比男神i
萌比男神i 2020-11-21 04:15

I want to convert a table, represented as a list of lists, into a Pandas DataFrame. As an extremely simplified example:

a = [[\'a\', \'1.2\', \'         


        
9条回答
  •  有刺的猬
    2020-11-21 05:10

    I thought I had the same problem but actually I have a slight difference that makes the problem easier to solve. For others looking at this question it's worth checking the format of your input list. In my case the numbers are initially floats not strings as in the question:

    a = [['a', 1.2, 4.2], ['b', 70, 0.03], ['x', 5, 0]]
    

    but by processing the list too much before creating the dataframe I lose the types and everything becomes a string.

    Creating the data frame via a numpy array

    df = pd.DataFrame(np.array(a))
    
    df
    Out[5]: 
       0    1     2
    0  a  1.2   4.2
    1  b   70  0.03
    2  x    5     0
    
    df[1].dtype
    Out[7]: dtype('O')
    

    gives the same data frame as in the question, where the entries in columns 1 and 2 are considered as strings. However doing

    df = pd.DataFrame(a)
    
    df
    Out[10]: 
       0     1     2
    0  a   1.2  4.20
    1  b  70.0  0.03
    2  x   5.0  0.00
    
    df[1].dtype
    Out[11]: dtype('float64')
    

    does actually give a data frame with the columns in the correct format

提交回复
热议问题