<p><code>fillna</code>打算使用<code>value</code>填充,或者使用<code>*{‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None}*</code>方法之一</p>
<p><a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html" rel="nofollow noreferrer"><strong>From the docs you referenced</strong></a></p>
<blockquote>
<p><strong>value</strong> : <em>scalar, dict, Series, or DataFrame</em> </p>
<p>Value to use to fill holes (e.g. 0), alternately a dict/Series/DataFrame of values specifying which value to use for each index (for a Series) or column (for a DataFrame). (values not in the dict/Series/DataFrame will not be filled). This value cannot be a list.</p>
</blockquote>
<p><strong><em>回答</em></strong></p>
<p><code>fillna</code>不接受函数作为<code>value</code>参数的有效输入。但是,如果您有一个可以生成序列或数据帧的函数,那么您可以将所需的任何输入传递给该函数,并将结果传递给<code>fillna</code></p>
<hr/>
<p>示例</p>
<p><code>value</code>参数可以采用标量、字典、序列或数据帧。下面是一些看起来像什么的例子</p>
<p>考虑数据帧<code>df</code></p>
<pre><code>df = pd.DataFrame([
[1, None, 2, None],
[None, 3, None, 4],
[5, 6, None, None],
[None, None, 7, 8]
], list('ABCD'), list('WXYZ'))
print(df)
W X Y Z
A 1.0 NaN 2.0 NaN
B NaN 3.0 NaN 4.0
C 5.0 6.0 NaN NaN
D NaN NaN 7.0 8.0
</code></pre>
<p><strong><em>用标量填充</em></strong></p>
^{pr2}$
<p><strong><em>用字典填充</em></strong></p>
<pre><code>filler = dict(W=-9, X=-10, Y=-11, Z=-12)
df.fillna(filler)
W X Y Z
A 1.0 -10.0 2.0 -12.0
B -9.0 3.0 -11.0 4.0
C 5.0 6.0 -11.0 -12.0
D -9.0 -10.0 7.0 8.0
</code></pre>
<p><strong><em>用系列填充</em></strong></p>
<pre><code>filler = pd.Series(dict(W=-9, X=-10, Y=-11, Z=-12))
df.fillna(filler)
W X Y Z
A 1.0 -10.0 2.0 -12.0
B -9.0 3.0 -11.0 4.0
C 5.0 6.0 -11.0 -12.0
D -9.0 -10.0 7.0 8.0
</code></pre>
<p><strong><em>填充数据帧</em></strong></p>
<pre><code>filler = pd.DataFrame(-np.arange(df.size).reshape(df.shape), df.index, df.columns)
print(filler)
W X Y Z
A 0 -1 -2 -3
B -4 -5 -6 -7
C -8 -9 -10 -11
D -12 -13 -14 -15
df.fillna(filler)
W X Y Z
A 1.0 -10.0 2.0 -12.0
B -9.0 3.0 -11.0 4.0
C 5.0 6.0 -11.0 -12.0
D -9.0 -10.0 7.0 8.0
</code></pre>
<p><strong><em>用along索引填充,您需要转置</em></strong><br/>
<code>df.fillna(filler, axis=1)</code>生成<code>NotImplementedError</code></p>
<pre><code>filler = dict(A=-9, B=-10, C=-11, D=-12)
print(df.T.fillna(filler).T)
W X Y Z
A 1.0 -9.0 2.0 -9.0
B -10.0 3.0 -10.0 4.0
C 5.0 6.0 -11.0 -11.0
D -12.0 -12.0 7.0 8.0
</code></pre>