Reputation: 19027
Supposed I have this DataFrame:
import numpy as np
df = pd.DataFrame({'cat':['foo', 'bar','foo'],
'val': [1,2,3]})
df['cat'] = df['cat'].astype('category')
So, its dtypes
(df.dtypes
) correspond to:
cat category
val int64
dtype: object
One option would be to simply use the columns:
new_df = pd.DataFrame(columns = df.columns)
However, this would not preserve the dtypes
(new_df.dtypes
):
cat object
val object
dtype: object
Upvotes: 1
Views: 714
Reputation: 20669
You can do this way. You can use pandas.DataFrame.astype
new_df = pd.dataFrame(columns = df.columns).astype(dtype = df.dtypes)
new_df.dtypes
cat category
val int64
dtype: object
Upvotes: 2
Reputation: 19027
You can just copy an empty version of the initial DataFrame. In the example you provided, just do this:
new_df = df[df.index != df.index].copy()
Running new_df.dtypes
should give you the expected categories:
cat category
val int64
dtype: object
Upvotes: 1