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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With