Is there a way to copy only the structure (not the data) of a Pandas DataFrame?

后端 未结 8 1962
感动是毒
感动是毒 2020-12-13 08:29

I received a DataFrame from somewhere and want to create another DataFrame with the same number and names of columns and rows (indexes). For example, suppose that the origin

8条回答
  •  再見小時候
    2020-12-13 08:40

    I know this is an old question, but I thought I would add my two cents.

    def df_cols_like(df):
        """
        Returns an empty data frame with the same column names and types as df
        """
        df2 = pd.DataFrame({i[0]: pd.Series(dtype=i[1])
                            for i in df.dtypes.iteritems()},
                           columns=df.dtypes.index)
        return df2
    

    This approach centers around the df.dtypes attribute of the input data frame, df, which is a pd.Series. A pd.DataFrame is constructed from a dictionary of empty pd.Series objects named using the input column names with the column order being taken from the input df.

提交回复
热议问题