我有:
import pandas as pd
import numpy as np
np.random.seed([3,1415])
df = pd.DataFrame(np.random.choice((1, 2, np.nan), (5, 5)))
s = pd.Series(range(5))
我想将 s
添加到 df
并跨行广播。通常我会:
df.add(s)
并完成它。但是,我想用 0
填充缺失值。所以我想我会:
df.add(s, fill_value=0)
但是我得到了:
NotImplementedError: fill_value 0 not supported
问题:如何获得预期的结果?
0 1 2 3 4
0 1.0 1.0 2.0 3.0 4.0
1 2.0 3.0 2.0 4.0 4.0
2 1.0 1.0 3.0 4.0 4.0
3 1.0 1.0 2.0 4.0 6.0
4 1.0 3.0 4.0 3.0 5.0
最佳答案
堆栈跟踪表明此参数从未实现:
.../pandas/core/frame.py in _combine_match_columns(self, other, func, level, fill_value)
3470 if fill_value is not None:
3471 raise NotImplementedError("fill_value %r not supported" %
-> 3472 fill_value)
3473
我可以在添加之前填充缺失值:
In [43]: df.fillna(0).add(s)
Out[43]:
0 1 2 3 4
0 1 1 2 3 4
1 2 3 2 4 4
2 1 1 3 4 4
3 1 1 2 4 6
4 1 3 4 3 5
关于python - pandas DataFrame 添加 fill_value NotImplementedError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37887796/