Create Empty Dataframe in Pandas specifying column types

前端 未结 11 1975
萌比男神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:02

    My solution (without setting an index) is to initialize a dataframe with column names and specify data types using astype() method.

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

提交回复
热议问题