0

I have a dataframe that looks something like this:

    wavelength  normalized flux lof
0   5100.00 0.948305    1
1   5100.07 0.796783    1
2   5100.14 0.696425    1
3   5100.21 0.880586    1
4   5100.28 0.836257    1
... ... ... ...
4281    5399.67 1.076449    1
4282    5399.74 1.038198    1
4283    5399.81 1.004292    1
4284    5399.88 0.946977    1
4285    5399.95 0.894559    1

If lof = -1, I want to replace the normalized flux value with np.nan. Otherwise, just leave the normalized flux value as is. Is there a simple way to do this?

Dila
  • 649
  • 1
  • 10

2 Answers2

0

You can just assign

df.loc[df['lof']==-1,'flux'] = np.nan
BENY
  • 317,841
  • 20
  • 164
  • 234
  • I think we can close as dupe? https://stackoverflow.com/questions/28896769/vectorize-conditional-assignment-in-pandas-dataframe – Umar.H Nov 02 '22 at 22:48
0
df['flux']=df['flux'].mask(df['lof'].eq(-1), np.nan)
df
Naveed
  • 11,495
  • 2
  • 14
  • 21
  • Please avoid providing code-only answers without an explanation on why your code resolves the question. While the code may solve the problem, the answer is not useful in understanding why it solves it. – Marc Sances Nov 03 '22 at 10:42