简体   繁体   English

如果特定列的一个值为 NaN,有没有办法使用 ffill 替换整个 pandas dataframe 行?

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

相关问题 当列值匹配时,Pandas Dataframe会从行中替换Nan - Pandas Dataframe replace Nan from a row when a column value matches 如何使用 Pandas 根据同一行中另一列的值替换一列中的 NaN 值? - How to replace NaN value in one column based on the value of another column in the same row using Pandas? Pandas Dataframe - 如果列值条件,将 NaN 替换为 0 - Pandas Dataframe - replace NaN with 0 if column value condition 如果一个值是 NaN,Pandas 用 NaN 替换一行中的所有项目 - Pandas replace all items in a row with NaN if one value is NaN 如果一个值是NaN,则熊猫用NaN替换一行中的所有值 - Pandas replace all values in a row with NaN if one value is NaN Python Pandas 将一列中的 NaN 替换为与列表列相同行的另一列中的值 - Python Pandas replace NaN in one column with value from another column of the same row it has be as list column 使用 Pandas 将特定列值替换为另一个数据框列值 - Replace specific column values with another dataframe column value using Pandas 熊猫:如果数据框列为“ NaN”,则替换该列 - Pandas: Replace dataframe column if it is `NaN` 根据 pandas dataframe 中的相邻列将 NaN 值替换为特定文本 - Replace NaN values with specific text based on adjacent column in pandas dataframe Python Pandas 用另一列下一行的值替换一列中的 NaN - Python Pandas replace NaN in one column with value from a row below of another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM