Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

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 original data frame was created as

import pandas as pd

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

I copied the structure by explicitly defining the columns and names:

df2 = pd.DataFrame(columns=df1.columns, index=df1.index)    

I don't want to copy the data, otherwise I could just write df2 = df1.copy(). In other words, after df2 being created it must contain only NaN elements:

In [1]: df1

Out[1]: 

    c1  c2

i1  11  12

i2  21  22

In [2]: df2

Out[2]: 

     c1   c2

i1  NaN  NaN

i2  NaN  NaN

Is there a more idiomatic way of doing it?

1 Answer

0 votes
by (41.4k points)

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

If you want to learn more about Pandas then visit this Python Course designed by the industrial experts.

Related questions

Browse Categories

...