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

后端 未结 8 1939
感动是毒
感动是毒 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 09:00

    That's a job for reindex_like. Start with the original:

    df1 = pd.DataFrame([[11, 12], [21, 22]], columns=['c1', 'c2'], index=['i1', 'i2'])
    

    Construct an empty DataFrame and reindex it like df1:

    pd.DataFrame().reindex_like(df1)
    Out: 
        c1  c2
    i1 NaN NaN
    i2 NaN NaN   
    
    0 讨论(0)
  • 2020-12-13 09:04

    This has worked for me in pandas 0.22: df2 = pd.DataFrame(index=df.index.delete(slice(None)), columns=df.columns)

    Convert types: df2 = df2.astype(df.dtypes)

    delete(slice(None)) In case you do not want to keep the values ​​of the indexes.

    0 讨论(0)
提交回复
热议问题