Home > Net >  How to modify code in Python so as to make calculations only on NOT NaN rows in Pandas?
How to modify code in Python so as to make calculations only on NOT NaN rows in Pandas?

Time:10-18

I have Pandas Data Frame in Python like below:

NR
--------
910517196
921122192
NaN

And by using below code I try to calculate age based on column NR in above Data Frame (it does not matter how below code works, I know that it is correct - briefly I take 6 first values to calculate age, because for example 910517 is 1991-05-17 :)):

df["age"] = (ABT_DATE - pd.to_datetime(df.NR.str[:6], format = '%y%m%d')) / np.timedelta64(1, 'Y')

My problem is: I can modify above code to calculate age only using NOT NaN values in column "NR" in Data Frame, nevertheless some values are NaN.

My question is: How can I modify my code so as to take to calculations only these rows from column "NR" where is not NaN ??

As a result I need something like below, so simply I need to temporarily disregard NaN rows and, where there is a NaN in column NR, insert also a NaN in the calculated age column:

NR         age
------------------
910517196 | 30
921122192 | 29
NaN       | NaN

How can I do that in Python Pandas ?

CodePudding user response:

df['age']=np.where(df['NR'].notnull(),'your_calculation',np.nan)
  • Related