pandas 减去数据帧熊猫时的NaN
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40654802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NaNs when subtracting dataframes pandas
提问by Marcus H?gen? Bohman
I have two dataframes with only somewhat overlapping indices and columns.
我有两个数据框,它们的索引和列只有一些重叠。
old = pd.DataFrame(index = ['A', 'B', 'C'],
columns = ['k', 'l', 'm'],
data = abs(np.floor(np.random.rand(3, 3)*10)))
new = pd.DataFrame(index = ['A', 'B', 'C', 'D'],
columns = ['k', 'l', 'm', 'n'],
data = abs(np.floor(np.random.rand(4, 4)*10)))
I want to calculate the difference between them and tried
我想计算它们之间的差异并尝试
delta = new - old
This gives lots of NaNs where indices and columns do not match. I would like to treat the abscence of the indices and columns as zeroes, (old['n', 'D'] = 0). old will always be a subspace of new.
这给出了许多索引和列不匹配的 NaN。我想将索引和列的缺失视为零,(old['n', 'D'] = 0)。old 永远是 new 的子空间。
Any ideas?
有任何想法吗?
EDIT: I guess I didn't explain it thoroughly enough. I don't want to fill the delta dataframe with zeroes. I want to treat missing indices and columns in old as if they were zeroes. I would then get the value in new['n', 'D'] in delta instead of a NaN.
编辑:我想我没有足够彻底地解释它。我不想用零填充增量数据帧。我想将旧的缺失索引和列视为零。然后我会在 new['n', 'D'] 中获得 delta 而不是 NaN 中的值。
采纳答案by EdChum
Use sub
with fill_value=0
:
使用sub
有fill_value=0
:
In [15]:
old = pd.DataFrame(index = ['A', 'B', 'C'],
columns = ['k', 'l', 'm'],
data = abs(np.floor(np.random.rand(3, 3)*10)))
?
new = pd.DataFrame(index = ['A', 'B', 'C', 'D'],
columns = ['k', 'l', 'm', 'n'],
data = abs(np.floor(np.random.rand(4, 4)*10)))
delta = new.sub(old, fill_value=0)
delta
Out[15]:
k l m n
A 0 3 -9 7
B 0 -2 1 8
C -4 1 1 7
D 8 6 0 6