Pandas replace all non-NaN entries of a dataframe with 1 leave NaN alone

Viewed 1502

How can I replace all the non-NaN values in a pandas dataframe with 1 but leave the NaN values alone? This almost does what I'm looking for. The problem is it also makes NaN values 0. Then I have to reset them to NaN after.

I would like this

    a    b
0  NaN  QQQ
1  AAA  NaN
2  NaN  BBB

to become this

    a    b
0  NaN   1
1   1   NaN
2  NaN   1

This code is almost what I want

newdf = df.notnull().astype('int')

The above code does this

    a    b
0   0   1
1   1   0
2   0   1
2 Answers

One way would be to select all non-null values from the original data frame and set them to one:

df[df.notnull()] = 1

This solution on your data:

df = pd.DataFrame({'a': [np.nan, 'AAA', np.nan], 'b': ['QQQ', np.nan, 'BBB']})
df[df.notnull()] = 1

df 
    a   b
0   NaN 1
1   1   NaN
2   NaN 1

You can use np.where() with DataFrame.isna() to accomplish this

df=pd.DataFrame(data=[[1,np.NaN,5],
                      ['q',np.NaN,np.NaN],
                      ['7',{'a':1},np.NaN]],
                columns=['a','b','c'])

    a   b           c
0   1   NaN         5.0
1   q   NaN         NaN
2   7   {'a': 1}    NaN

df1=pd.DataFrame(np.where(df.isna(),df,1), columns=df.columns)

    a   b       c
0   1   NaN     1
1   1   NaN     NaN
2   1   1       NaN
Related