python - 当间隔的位置由索引列表定义时,如何对 pandas 数据框中的行间隔进行切片和应用函数?

标签 python pandas

我有一个像这样的数据框:

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/

相关文章:

python - 如何为用户模型 Django 创建自定义表单

带逗号的 Python 数组切片?

python - httplib2 使用代理给出内部服务器错误 500

python - 从 ordereddict 生成 Pandas 数据框?

Python - 如何检查系统负载?

Python Django 单元测试 : How to tear down fixtures

python - Pandas 交叉制表和计数

python - 需要函数源码从dataframe中获取数据;找到平均中位数和众数

python - 有没有一种方法可以返回只有一列不为空的所有行?

python - 遍历帐号列中的每一行并使用它们读取其他 Excel 文件