简体   繁体   中英

How to create a new dataframe based on dtypes from an existing dataframe?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM