Assign pandas dataframe column dtypes

后端 未结 6 668
醉梦人生
醉梦人生 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 07:01

    You're better off using typed np.arrays, and then pass the data and column names as a dictionary.

    import numpy as np
    import pandas as pd
    # Feature: np arrays are 1: efficient, 2: can be pre-sized
    x = np.array(['a', 'b'], dtype=object)
    y = np.array([ 1 ,  2 ], dtype=np.int32)
    df = pd.DataFrame({
       'x' : x,    # Feature: column name is near data array
       'y' : y,
       }
     )
    

提交回复
热议问题