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

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

    You can simply mask by notna() i.e

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

提交回复
热议问题