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

后端 未结 8 1965
感动是毒
感动是毒 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:46

    A simple alternative -- first copy the basic structure or indexes and columns with datatype from the original dataframe (df1) into df2

    df2 = df1.iloc[0:0]
    

    Then fill your dataframe with empty rows -- pseudocode that will need to be adapted to better match your actual structure:

    s = pd.Series([Nan,Nan,Nan], index=['Col1', 'Col2', 'Col3'])
    

    loop through the rows in df1

    df2 = df2.append(s)
    

提交回复
热议问题