Create Empty Dataframe in Pandas specifying column types

前端 未结 11 1980
萌比男神i
萌比男神i 2020-11-28 08:39

I\'m trying to create an empty data frame with an index and specify the column types. The way I am doing it is the following:

df = pd.DataFrame(index=[\'pbp\         


        
11条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 09:11

    You can use the following:

    df = pd.DataFrame({'a': pd.Series([], dtype='int'),
                       'b': pd.Series([], dtype='str'),
                       'c': pd.Series([], dtype='float')})
    

    then if you call df you have

    >>> df 
    Empty DataFrame 
    Columns: [a, b, c]
    Index: []
    

    and if you check its types

    >>> df.dtypes
    a      int32
    b     object
    c    float64
    dtype: object
    

提交回复
热议问题