2

I have the following dataframe in:

  a
1 3
2 2
3 Nan
4 3
5 Nan

I need to recode this column so it looks like this:

  df_miss_a
1 0
2 0
3 1
4 0
5 1

I've tried:

df_miss_a = np.where(df['a'] == 'Nan', 1, 0)

and

df_miss_a = np.where(df['a'] == Nan, 1, 0)

The above outputs only 0s.

The format of the output is unimportant.

3
  • @mkrieger1 amended per your suggestions. Commented Apr 29, 2024 at 21:34
  • Does this answer your question? Pandas Replace NaN with blank/empty string Commented Apr 29, 2024 at 21:35
  • @ti7 It doesn't, because the solutions suggested there (using replace or fillna) only solve part of OP's problem; namely replacing all values that are NaN. However, OP also wants to replace all values that are not NaN. Commented Apr 30, 2024 at 12:51

1 Answer 1

4

If you have NaNs in your column you can use pd.Series.isna():

df_miss_a = df["a"].isna().astype(int)
print(df_miss_a)

Prints:

1    0
2    0
3    1
4    0
5    1
Name: a, dtype: int64
Sign up to request clarification or add additional context in comments.

3 Comments

pd.Series.fillna(0) is simpler and more obvious
@DeusXMachina pd.Series.fillna(0) would not work: (1) It sets NaN to 0 rather than 1, as required by OP. Of course, this could easily be mitigated by pd.Series.fillna(1); however, this would leave the 2nd problem: (2) OP wants all values that are not NaN to be set to 0. Your solution with fillna() would simply leave them alone, so you would require an extra step, while Andrej Kesely's answer solves both issues at once.
Ah, thanks for the clarification. I read it too fast.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.