[英]How to modify code in Python so as to make calculations only on NOT NaN rows in Pandas?
I have Pandas Data Frame in Python like below:我在 Python 中有 Pandas 数据框,如下所示:
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 :)):通过使用下面的代码,我尝试根据上面数据框中的 NR 列计算年龄(下面的代码如何工作无关紧要,我知道它是正确的 - 简单地说,我采用 6 个第一个值来计算年龄,因为例如 910517 是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.我的问题是:我可以修改上面的代码以仅使用数据框中“NR”列中的 NOT NaN 值来计算年龄,但有些值是 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 ??我的问题是:如何修改我的代码以便仅计算列“NR”中的这些行,其中不是 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:因此,我需要类似下面的内容,所以我只需要暂时忽略 NaN 行,并且在 NR 列中存在 NaN 的情况下,在计算的年龄列中也插入一个 NaN:
NR age
------------------
910517196 | 30
921122192 | 29
NaN | NaN
How can I do that in Python Pandas ?我怎样才能在 Python Pandas 中做到这一点?
df['age']=np.where(df['NR'].notnull(),'your_calculation',np.nan)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.