user21
user21

Reputation: 329

Python pandas/numpy fill 1 to cells with value, and zero to cells of nan

I have an array with cells of different types of data (String, float, Integer, ...) .

e.g.

[[18 '1/4/11' 73.0 'Male' 4.0]
 [18    nan   73.0 'Male' nan]
 [18 '7/5/11' 73.0 'Male' 7.0]]

And I want to assign 0 to cells with value nan, and 1 to all others

expected outcome:

[[1 1 1 1 1
  1 0 1 1 0
  1 1 1 1 1]]

With pandas's fillna(0), I'm able to fill nan with 0, but how to assign 1 to all the cells with available values given that the data is of different types?

Upvotes: 1

Views: 61

Answers (2)

jezrael
jezrael

Reputation: 862771

Create boolean mask and cast to integer:

~(np.isnan(arr)).astype(int)

Or:

 pd.notnull(arr).astype(int)

Upvotes: 0

Sebastian Mendez
Sebastian Mendez

Reputation: 2981

Whether it's a dataframe or an ndarray, you can use pd.notnull:

>>> arr = np.array([[18, '1/4/11', 73.0, 'Male', 4.0],
...                 [18,    np.nan,   73.0, 'Male', np.nan],
...                 [18, '7/5/11', 73.0, 'Male', 7.0]], dtype=object)
>>> pd.notnull(arr)
array([[ True,  True,  True,  True,  True],
       [ True, False,  True,  True, False],
       [ True,  True,  True,  True,  True]], dtype=bool)

Upvotes: 1

Related Questions