0

I want to check if either column contains a nan and if so the value in my new column to be "na" else 1.

I was able to get my code to work when checking a single column using .isnull() but I am unsure how to combine two. I tried using or as seen below. But it did not work. I know I can make the code a bit messy by testing one condition and then checking the next and from that producing the outcome I want but was hoping to make the code a bit cleaner by using some sort of any, or etc function instead but I do not know how to do that.

temp_df["xl"] = np.where(temp_df["x"].isnull() or temp_df["y"].isnull(), "na",1)
JPWilson
  • 691
  • 4
  • 14
  • 1
    Use `temp_df["x"].isnull()|temp_df["y"].isnull()`. – Henry Yik Oct 18 '20 at 15:14
  • Can I Just ask what does the | do exactly does it just mean or? I tried to google but nothing came up for it – JPWilson Oct 18 '20 at 15:17
  • It is bitwise operator `or`. See [this](https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#boolean-indexing) for the official pandas doc on boolean indexing. – Henry Yik Oct 18 '20 at 15:52

1 Answers1

1

Let us do any more detail explain the situation you have link

temp_df["xl"] = np.where(temp_df[['x','y']].isnull().any(1), "na",1)
BENY
  • 317,841
  • 20
  • 164
  • 234