[英]Slicing data with Multiindex columns
我有一個帶有MultiIndex列的數據框。 我想使用數據集的列過濾數據。 當我嘗試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]
確實說這是[529757 rows x 1 columns]
的數據集。
我想要做的是選擇一個時間間隔來過濾數據,選擇第一列稱為physical_quantity type 。
另一方面, 第一列的數據( physical_quantity type )是未知的:
physical_quantity type
power active float32
dtype: object
檢查與df.index
我設法看到有關數據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
我該如何解決我的問題?
嘗試這個
df['ts'] = pd.to_datetime(df["ts"], unit="ms")```
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.