简体   繁体   English

如何修改 Python 中的代码以便仅对 Pandas 中的 NOT NaN 行进行计算?

[英]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.

相关问题 如何修改循环以便从 Pandas Python 中的 DataFrame 中的列中的值中获取 NaN 值? - How to modify loop so as to take NaN values from values in columns in DataFrame in Pandas Python? 如何修改 DataFrames 以便它们在 Pandas 中只有具有共享索引值的行? - How to modify DataFrames so that they only have rows with shared index values in Pandas? 忽略 pandas 计算中的“NaN”? - Ignore "NaN" in pandas calculations? 如何使用列上的尾随行在同一列上进行计算 熊猫蟒 - How to use trailing rows on a column for calculations on that same column | Pandas Python 如何在Python熊猫中修改重复的行 - How to modify duplicated rows in Python pandas Python / Pandas:如何使用NaN合并不同行中的重复行? - Python/Pandas: How to consolidate repeated rows with NaN in different columns? Python,Pandas-参考其他行的计算 - Python, Pandas - Calculations with referances to other rows 如何修改函数以根据 Python Pandas 中的值返回 2 个 DataFrame? - How to modify function so as to return 2 DataFrame depending on values in Python Pandas? Pandas - 删除只有 NaN 值的行 - Pandas - Delete Rows with only NaN values Python:Pandas - 仅删除 NaN 行并向上移动数据,不向上移动具有部分 NaN 的行中的数据 - Python : Pandas - ONLY remove NaN rows and move up data, do not move up data in rows with partial NaNs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM