- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个带有 MultiIndex 的 DataFrame,如下所示:
col col col col ...
tstp pkt
2016-04-14 04:05:32.321 0 ... ... ... ...
25 ... ... ... ...
2016-04-14 04:05:32.322 1 ... ... ... ...
26 ... ... ... ...
2016-04-14 04:05:32.374 2 ... ... ... ...
...
beg
和
end
, 我想用
df[].between_time(beg,end)
从 DataFrame 中获取相关行。唯一的问题是,
.between_time(beg,end)
似乎只适用于 DateTimeIndex:
*** TypeError: Index must be DatetimeIndex
xs()
执行此操作的更合适的方法?
df.xs(slice(beg,end),level='tstp')
最佳答案
有多种方法可以获得您想要的结果:
选项1
可能最好的方法是使用 DataFrame.loc
直接索引到 MutliIndex
:
df.loc[beg:end]
between_time
, 你可以
unstack
将第二级索引取出,然后使用
between_time
最后
stack
第二层回:
df.unstack().between_time(beg,end).stack()
xs
会给你一个类似的结果:
df.xs(slice(beg,end),level='tstp')
>>> timeit df.loc[beg:end]
1000 loops, best of 3: 317 µs per loop
>>> timeit df.unstack().between_time(beg,end).stack()
100 loops, best of 3: 3.35 ms per loop
>>> timeit df.xs(slice(beg,end),level='tstp')
1000 loops, best of 3: 632 µs per loop
关于python - 在 MultiIndex 上使用 between_time()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36614427/
考虑一下: import pandas as pd import numpy as np idx2=[pd.to_datetime('2016-08-31 22:08:12.000'), p
假设我有一个带有 MultiIndex 的 DataFrame,如下所示: col col col col ... tstp
我尝试过滤 pandas 中的数据集,以仅获取属于特定时间段列表内的数据。我尝试在以下数据集上进行数据分析: data csv 此外,开始和结束时间作为以下 .csv 文件中的一列: csv spec
我正在尝试创建一个列,如果行值介于 09:00 和 17:00 之间,该列将赋值为 true。 我可以使用 between_time 轻松选择这些时间,但无法为新列分配 True、False。 df
我需要过滤掉特定时间的数据。 DataFrame 函数 between_time 似乎是正确的方法,但是,它只适用于数据帧的索引列;但我需要原始格式的数据(例如,数据透视表希望日期时间列具有正确的名称
我正在尝试使用 between_time 函数。我已经将字符串类型时间格式化为日期时间 dataset['TimeStamp'] = pd.to_datetime(dataset['TimeStamp
我有一个 pandas df,我使用 between_time a 和 b 来清理数据。我如何获得非_between_time 行为? 我知道我可以尝试类似的东西。 df.between_time['
这个问题在这里已经有了答案: Pandas: select DF rows based on another DF (5 个答案) 关闭 5 年前。 如果我有一个包含开始时间和结束时间的 DataF
我坚持使用 pandas 0.9.0,因为我在 python 2.5 下工作,因此我没有 between_time方法可用。 我有一个日期数据框,想过滤特定时间之间的所有日期,例如对于 DataFra
我是一名优秀的程序员,十分优秀!