从DataFrame中减去Series,同时保持DataFrame结构完整 [英] Subtract a Series from a DataFrame while keeping the DataFrame struct intact
本文介绍了从DataFrame中减去Series,同时保持DataFrame结构完整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在保持完整的DataFrame结构的同时从DataFrame中减去系列?
How can I subtract a Series from a DataFrame, while keeping the DataFrame struct intact?
df = pd.DataFrame(np.zeros((5,3)))
s = pd.Series(np.ones(5))
df - s
0 1 2 3 4
0 -1 -1 -1 NaN NaN
1 -1 -1 -1 NaN NaN
2 -1 -1 -1 NaN NaN
3 -1 -1 -1 NaN NaN
4 -1 -1 -1 NaN NaN
我想要拥有的等效于从DataFrame中减去标量
What I would like to have is the equivalent of subtracting a scalar from the DataFrame
df - 1
0 1 2
0 -1 -1 -1
1 -1 -1 -1
2 -1 -1 -1
3 -1 -1 -1
4 -1 -1 -1
推荐答案
也许:
>>> df = pd.DataFrame(np.zeros((5,3)))
>>> s = pd.Series(np.ones(5))
>>> df.sub(s,axis=0)
0 1 2
0 -1 -1 -1
1 -1 -1 -1
2 -1 -1 -1
3 -1 -1 -1
4 -1 -1 -1
[5 rows x 3 columns]
,或者,举一个更有趣的例子:
or, for a more interesting example:
>>> s = pd.Series(np.arange(5))
>>> df.sub(s,axis=0)
0 1 2
0 0 0 0
1 -1 -1 -1
2 -2 -2 -2
3 -3 -3 -3
4 -4 -4 -4
[5 rows x 3 columns]
这篇关于从DataFrame中减去Series,同时保持DataFrame结构完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文