大熊猫按iloc减去系列,而不是索引

时间:2018-04-16 16:28:39

标签: python pandas

我有两个pd.Series

>>> a = pd.Series([1,2,3],index=[1,2,3])
>>> b = pd.Series([2,3,4],index=[2,3,4])

我想根据元素'.iloc减去这两个系列,而不是索引,然后返回第一个(或第二个,我不在乎,真的){{1 }}

期望的输出:

Series

实际出现的是:

>>> a - b
1    -1
2    -1
3    -1
dtype: float64

2 个答案:

答案 0 :(得分:3)

您可以通过访问numpy数组表示来执行此操作:

res = pd.Series(a.values - b.values, index=a.index)

print(res)

# 1   -1
# 2   -1
# 3   -1
# dtype: int64

答案 1 :(得分:2)

我建议

@DSM's comment 但是,我将展示如何为pandas.Series

执行此操作
a.values[:] -= b.values
a

1   -1
2   -1
3   -1
dtype: int64

你也可以做同样的事情:

a.loc[:] -= b.values

或者

a.iloc[:] -= b.values

使用lociloc是更惯用的Pandas。

相关问题