[英]Is there a way to replace a whole pandas dataframe row using ffill, if one value of a specific column is NaN?
I am trying to sort a dataframe where some rows are all NaN.我正在尝试对 dataframe 进行排序,其中一些行都是 NaN。 I want to fill these using ffill.我想用 ffill 填充这些。 I'm currently trying this although i feel like it's a mismatch of a few commands我目前正在尝试这个,虽然我觉得这是一些命令的不匹配
df.loc[df['A'].isna(), :] = df.fillna(method='ffill')
This gives an error: AttributeError: 'NoneType' object has no attribute 'fillna'
这给出了一个错误: AttributeError: 'NoneType' object has no attribute 'fillna'
but I want to filter the NaNs I fill using ffill if one of the columns is NaN.但如果其中一列是 NaN,我想过滤使用 ffill 填充的 NaN。 ie IE
A B C D E
0 45 88 NaN NaN 3
1 62 34 2 86 NaN
2 85 65 11 31 5
3 NaN NaN NaN NaN NaN
4 90 38 34 93 8
5 0 94 45 10 10
6 58 NaN 23 60 11
7 10 32 5 15 11
8 NaN NaN NaN NaN NaN
So I would only like to fill a row IFF the value of A is NaN, whilst leaving C,0 and D,0 as NaN.所以我只想填充一行 IFF A 的值为 NaN,同时将 C,0 和 D,0 保留为 NaN。 Giving the below dataframe给出以下 dataframe
A B C D E
0 45 88 NaN NaN 3
1 62 34 2 86 NaN
2 85 65 11 31 5
3 85 65 11 31 5
4 90 38 34 93 8
5 0 94 45 10 10
6 58 NaN 23 60 11
7 10 32 5 15 11
8 10 32 5 15 11
So just to clarify, the ONLY rows that get replaced with ffill are 3,8 and the reason is because the value of column A in rows 3 and 8 are NaN Thanks所以只是为了澄清,被 ffill 替换的唯一行是 3,8 原因是因为第 3 行和第 8 行中 A 列的值是 NaN 谢谢
---Update--- When I'm debugging and evaluate the expression: df.loc[df['A'].isna(), :]
---更新---当我调试和评估表达式时: df.loc[df['A'].isna(), :]
I get我明白了
3 NaN NaN NaN NaN NaN
8 NaN NaN NaN NaN NaN
So I assume whats happening here is, I then attempt ffill on this new dataframe only containing 3 and 8 and obviously i cant ffill NaNs with NaNs.所以我假设这里发生的事情是,然后我尝试填充这个仅包含 3 和 8 的新 dataframe ,显然我不能用 NaN 填充 NaN。
Change values only to those row that start with nan仅将值更改为以 nan 开头的行
df.loc[df['A'].isna(), :] = df.ffill().loc[df['A'].isna(), :]
A B C D E
0 45.0 88.0 NaN NaN 3.0
1 62.0 34.0 2.0 86.0 NaN
2 85.0 65.0 11.0 31.0 5.0
3 85.0 65.0 11.0 31.0 5.0
4 90.0 38.0 34.0 93.0 8.0
5 0.0 94.0 45.0 10.0 10.0
6 58.0 NaN 23.0 60.0 11.0
7 10.0 32.0 5.0 15.0 11.0
8 10.0 32.0 5.0 15.0 11.0
Try using a mask to identify the relevant rows where column A
is null.尝试使用掩码来识别列A
为 null 的相关行。 The take those same rows from the forward filled dataframe.从前向填充的 dataframe 中获取相同的行。
mask = df['A'].isnull()
df.loc[mask, :] = df.ffill().loc[mask, :]
>>> df
A B C D E
0 45.0 88.0 NaN NaN 3.0
1 62.0 34.0 2.0 86.0 NaN
2 85.0 65.0 11.0 31.0 5.0
3 85.0 65.0 11.0 31.0 5.0
4 90.0 38.0 34.0 93.0 8.0
5 0.0 94.0 45.0 10.0 10.0
6 58.0 NaN 23.0 60.0 11.0
7 10.0 32.0 5.0 15.0 11.0
8 10.0 32.0 5.0 15.0 11.0
you just want to fill ( DataFrame.ffill
) where ( DataFrame.where
) df['A']
is nan
and the rest leave it as before ( df ):您只想填写( DataFrame.ffill
)其中( DataFrame.where
) df['A']
是nan
并且rest离开它之前:
df=df.ffill().where(df['A'].isna(),df)
print(df)
A B C D E
0 45.0 88.0 NaN NaN 3.0
1 62.0 34.0 2.0 86.0 NaN
2 85.0 65.0 11.0 31.0 5.0
3 85.0 65.0 11.0 31.0 5.0
4 90.0 38.0 34.0 93.0 8.0
5 0.0 94.0 45.0 10.0 10.0
6 58.0 NaN 23.0 60.0 11.0
7 10.0 32.0 5.0 15.0 11.0
8 10.0 32.0 5.0 15.0 11.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.