python - 使用 2 个复合索引对 Pandas DataFrame 进行切片

标签 python python-2.7 pandas

在以时间戳作为复合索引的 DataFrame 中,我们如何对满足这两个条件的行进行切片:

  • start 早于 2014-09-26 12:00:00
  • 停止2014-09-26 13:00:00
  • 之后

df 的索引 startstop 原本是普通列,设置为索引是为了方便切片,就像只有 1 个索引的原因:

                                                jobId
        start               stop                              
2014-09-26 09:45:01 2014-09-26 09:48:02           c35877  
2014-09-26 11:23:46 2014-09-26 11:53:13           8f0f64  
2014-09-26 11:46:50 2014-09-26 11:58:07           52ac37  
2014-09-26 12:10:15 2014-09-26 12:23:23           47dfc2  
2014-09-26 12:13:52 2014-09-26 12:18:31           c35877  
2014-09-26 12:30:47 2014-09-26 12:39:49           8f0f64  
2014-09-26 12:37:53 2014-09-26 12:45:48           96b20b  
2014-09-26 12:45:35 2014-09-26 12:50:22           8f0f64  
2014-09-26 12:49:26 2014-09-26 13:03:59           285618  
2014-09-26 13:04:42 2014-09-26 13:15:23           2c74a9  
2014-09-26 13:20:01 2014-09-26 13:27:46           8f0f64

最佳答案

使用查询方法(最近版本):

import pandas
from io import StringIO

rawdata = StringIO("""start,stop,jobID
2014-09-26 09:45:01,2014-09-26 09:48:02,c35877
2014-09-26 11:23:46,2014-09-26 11:53:13,8f0f64
2014-09-26 11:46:50,2014-09-26 11:58:07,52ac37
2014-09-26 12:10:15,2014-09-26 12:23:23,47dfc2
2014-09-26 12:13:52,2014-09-26 12:18:31,c35877
2014-09-26 12:30:47,2014-09-26 12:39:49,8f0f64
2014-09-26 12:37:53,2014-09-26 12:45:48,96b20b
2014-09-26 12:45:35,2014-09-26 12:50:22,8f0f64
2014-09-26 12:49:26,2014-09-26 13:03:59,285618
2014-09-26 13:04:42,2014-09-26 13:15:23,2c74a9
2014-09-26 13:20:01,2014-09-26 13:27:46,8f0f64
""")
df = pandas.read_csv(rawdata, parse_dates=True, index_col=['start', 'stop'])

df.query("start > '2014-09-26 12:00:00' and stop < '2014-09-26 13:00:00'")

打印:

                                          jobID
start               stop                       
2014-09-26 12:10:15 2014-09-26 12:23:23  47dfc2
2014-09-26 12:13:52 2014-09-26 12:18:31  c35877
2014-09-26 12:30:47 2014-09-26 12:39:49  8f0f64
2014-09-26 12:37:53 2014-09-26 12:45:48  96b20b
2014-09-26 12:45:35 2014-09-26 12:50:22  8f0f64

关于python - 使用 2 个复合索引对 Pandas DataFrame 进行切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26475406/

相关文章:

python - 如何修复 AWS lambda 中的 "Unable to import module"错误

python - 在 pythonbrew 中使用 virtualenv |安装依赖项

python - 将列名添加到 panda 数据框(多索引)

python - 使用多列索引创建 Pandas Dataframe

python - array.array 插入在 python 中是如何工作的?

没有输出的 Python lambda 调用函数

python - 将 Pandas DataFrame 写入 MySQL 数据库

python - Python openCV:无法显示图像

python - 在 Python 中构建字符串的最佳方法

python - 以优化的方式查找 Pandas 列中连续增加/减少值的数量(并用它填充另一个列)