Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Empty Dataframe in Pandas specifying column types

Tags:

python

pandas

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'],                   columns=['contract',                            'state_and_county_code',                            'state',                            'county',                            'starting_membership',                            'starting_raw_raf',                            'enrollment_trend',                            'projected_membership',                            'projected_raf'],                   dtype=['str', 'str', 'str', 'str',                          'int', 'float', 'float',                          'int', 'float']) 

However, I get the following error,

TypeError: data type not understood 

What does this mean?

like image 638
Vincent Avatar asked Apr 06 '16 21:04

Vincent


People also ask

How do I create a blank DataFrame in pandas?

You can create an empty dataframe by importing pandas from the python library. Later, using the pd. DataFrame(), create an empty dataframe without rows and columns as shown in the below example.


1 Answers

You can use the following:

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

or more abstractly:

df = pd.DataFrame({c: pd.Series(dtype=t) for c, t in {'a': 'int', 'b': 'str', 'c': 'float'}.items()}) 

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 
like image 57
Alberto Avatar answered Oct 11 '22 19:10

Alberto