Change column type in pandas

前端 未结 9 1302
萌比男神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 04:57

    How about creating two dataframes, each with different data types for their columns, and then appending them together?

    d1 = pd.DataFrame(columns=[ 'float_column' ], dtype=float)
    d1 = d1.append(pd.DataFrame(columns=[ 'string_column' ], dtype=str))
    

    Results

    In[8}:  d1.dtypes
    Out[8]: 
    float_column     float64
    string_column     object
    dtype: object
    

    After the dataframe is created, you can populate it with floating point variables in the 1st column, and strings (or any data type you desire) in the 2nd column.

提交回复
热议问题