Assign pandas dataframe column dtypes

后端 未结 6 691
醉梦人生
醉梦人生 2020-12-02 06:03

I want to set the dtypes of multiple columns in pd.Dataframe (I have a file that I\'ve had to manually parse into a list of lists, as the file was

6条回答
  •  臣服心动
    2020-12-02 06:42

    Another way to set the column types is to first construct a numpy record array with your desired types, fill it out and then pass it to a DataFrame constructor.

    import pandas as pd
    import numpy as np    
    
    x = np.empty((10,), dtype=[('x', np.uint8), ('y', np.float64)])
    df = pd.DataFrame(x)
    
    df.dtypes ->
    
    x      uint8
    y    float64
    

提交回复
热议问题