我有一个像这样的数据框:
df1 = pd.DataFrame({'col1':range(1,10,1),'col2':range(100,1000,100)})
col1 col2
0 1 100
1 2 200
2 3 300
3 4 400
4 5 500
5 6 600
6 7 700
7 8 800
8 9 900
这些是我的间隔的开始和结束索引:
sta_idxs = pd.Series([3,6], index=['col1','col2'])
end_idxs = pd.Series([5,7], index=['col1','col2'])
sta_idxs end_idxs
col1 3 col1 5
col2 6 col2 7
dtype: int64 dtype: int64
我想对 col1 (4+5+6) 中索引 3 和 5 之间的数字以及 col2 (700+800) 中索引 6 和 7 之间的数字求和。 预期结果是:
col1 15
col2 1500
我正在寻找一种矢量化解决方案以避免循环列。
感谢任何帮助。
最佳答案
对于一般情况和有点过分的情况,我们可以使用滚动
和查找
:
pd.Series(df1.rolling(3).sum()
.lookup(end_idxs, end_idxs.index),
index=end_idxs.index)
输出:
col1 15.0
col2 2100.0
dtype: float64
关于python - 当间隔的位置由索引列表定义时,如何对 pandas 数据框中的行间隔进行切片和应用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60456406/