[英]Pandas set all values after first NaN to NaN
For each row I would like to set all values to NaN after the appearance of the first NaN.对于每一行,我想在第一个 NaN 出现后将所有值设置为 NaN。 Eg:
例如:
a b c
1 2 3 4
2 nan 2 nan
3 3 nan 23
Should become this:应该变成这样:
a b c
1 2 3 4
2 nan nan nan
3 3 nan nan
So far I only know how to do this with an apply with a for loop over each column per row - it's very slow!到目前为止,我只知道如何在每行的每一列上使用 for 循环来执行此操作 - 这非常慢!
Check with cumprod
检查
cumprod
df=df.where(df.notna().cumprod(axis=1).eq(1))
a b c
1 2.0 3.0 4.0
2 NaN NaN NaN
3 3.0 NaN NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.