fillna() 方法在 Pandas 中的行为 -- 不遵守 inplace 参数 - returns 轴 1 上的错误

fillna() method's behavior in Pandas -- does not obey to inplace argument - returns error on axis 1

我正在试验 fillna() 方法。为此,我创建了一个小数据框和两个系列:

        col1    col2    col3      col4
     0  NaN      NaN    3           4
     1  NaN      NaN    7           8
     2  9.0     10.0    11         12

     n1 = pd.Series([10, 20])
     n2 = pd.Series([30, 40, 50, 60])
     n2.index = list(df.columns.values)

当我尝试命令时:

    df.fillna(n1, axis=0, inplace = True)

没有任何反应,NaN 保持不变。我希望看到它们被替换为值 10 (col1) 和 20 (col2)。 当我尝试

    df.fillna(n2, axis =1)

我收到一条错误消息:

   NotImplementedError: Currently only can fill with dict/Series column by column

你能解释一下这种行为吗?我们将不胜感激您的建议。

fillna(与 pandas 中的许多其他方法一样)默认情况下未就位。您需要传递 inplace=True 或重新分配其 return 值。

df.fillna(n1, axis=0, inplace=True)

df = df.fillna(n1, axis=0)

在这种情况下,阅读文档会有所帮助:

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html

inplace : boolean, default False. If True, fill in place. Note: this will modify any other views on this object, (e.g. a no-copy slice for a column in a DataFrame)
.
.
Returns: filled : DataFrame