Create Empty Dataframe in Pandas specifying column types

前端 未结 11 1988
萌比男神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 08:55

    This is an old question, but I don't see a solid answer (although @eric_g was super close).

    You just need to create an empty dataframe with a dictionary of key:value pairs. The key being your column name, and the value being an empty data type.

    So in your example dataset, it would look as follows (pandas 0.25 and python 3.7):

    variables = {'contract':'',
                 'state_and_county_code':'',
                 'state':'',
                 'county':'',
                 'starting_membership':int(),
                 'starting_raw_raf':float(),
                 'enrollment_trend':float(),
                 'projected_membership':int(),
                 'projected_raf':float()}
    
    df = pd.DataFrame(variables, index=[])
    

    In old pandas versions, one may have to do:

    df = pd.DataFrame(columns=[variables])
    

提交回复
热议问题