Change column type in pandas

前端 未结 9 1336
萌比男神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:01

    pandas >= 1.0

    Here's a chart that summarises some of the most important conversions in pandas.

    Conversions to string are trivial .astype(str) and are not shown in the figure.

    "Hard" versus "Soft" conversions

    Note that "conversions" in this context could either refer to converting text data into their actual data type (hard conversion), or inferring more appropriate data types for data in object columns (soft conversion). To illustrate the difference, take a look at

    df = pd.DataFrame({'a': ['1', '2', '3'], 'b': [4, 5, 6]}, dtype=object)
    df.dtypes                                                                  
    
    a    object
    b    object
    dtype: object
    
    # Actually converts string to numeric - hard conversion
    df.apply(pd.to_numeric).dtypes                                             
    
    a    int64
    b    int64
    dtype: object
    
    # Infers better data types for object data - soft conversion
    df.infer_objects().dtypes                                                  
    
    a    object  # no change
    b     int64
    dtype: object
    
    # Same as infer_objects, but converts to equivalent ExtensionType
    df.convert_dtypes().dtypes                                                     
    

提交回复
热议问题