使用多索引列对数据进行切片

2024-06-12 00:59:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个多索引列的数据框架。我想使用数据集的列过滤数据。当我尝试df.columns时,我得到以下信息:

MultiIndex(levels=[['power'], ['active']],
           codes=[[0], [0]],
           names=['physical_quantity', 'type'])

数据集的简短描述如下:

physical_quantity          power
type                      active
2011-04-18 09:22:13-04:00    6.0
2011-04-18 09:22:16-04:00    6.0
2011-04-18 09:22:20-04:00    6.0
2011-04-18 09:22:23-04:00    6.0
2011-04-18 09:22:26-04:00    6.0

我发现的第一件事是,尽管您在那里看到了两列,但数据帧实际上表示它是一个[529757 rows x 1 columns]的数据集

我想做的是过滤数据,选择一个时间间隔,选择第一列,称为物理量类型

另一方面,第一列物理量类型)的数据未知:

physical_quantity  type  
power              active    float32
dtype: object

使用df.index检查时,我设法查看了有关数据帧的以下信息:

DatetimeIndex(['2011-04-18 09:22:13-04:00', '2011-04-18 09:22:16-04:00',
               '2011-04-18 09:22:20-04:00', '2011-04-18 09:22:23-04:00',
               '2011-04-18 09:22:26-04:00', '2011-04-18 09:22:30-04:00',
               '2011-04-18 09:22:33-04:00', '2011-04-18 09:22:37-04:00',
               '2011-04-18 09:22:40-04:00', '2011-04-18 09:22:44-04:00',
               ...
               '2011-05-14 23:59:26-04:00', '2011-05-14 23:59:29-04:00',
               '2011-05-14 23:59:33-04:00', '2011-05-14 23:59:36-04:00',
               '2011-05-14 23:59:40-04:00', '2011-05-14 23:59:43-04:00',
               '2011-05-14 23:59:46-04:00', '2011-05-14 23:59:50-04:00',
               '2011-05-14 23:59:53-04:00', '2011-05-14 23:59:57-04:00'],
              dtype='datetime64[ns, US/Eastern]', length=529757, freq=None)

所以我知道,的数据类似于dtype='datetime64[ns, US/Eastern]

所以我的目标是将数据从一个特定的日子和一个小时分割到另一个日子和一个小时

从2011-05-10 19:44:51-04:00到2011-05-10 23:17:59-04:00

我试过这样做:

df[df['physical_quantity', 'type']] > 2011-05-10 19:44:51-04:00 
& 
df[df['physical_quantity', 'type']] < 2011-05-10 23:17:59-04:00
df[df['physical_quantity', 'type']] > 2011-05-10 19:44:51-04:00

File "<ipython-input-133-27848c7d6afc>", line 1
    df[df['physical_quantity', 'type']] > 2011-05-10 19:44:51-04:00
                                                ^
SyntaxError: invalid token

我怎样才能解决我的问题


Tags: columns数据信息类型dftypequantityactive