我试图切入具有由IntervalIndex和常规索引组成的MultiIndex的DataFrame。示例代码:
from pandas import Interval as ntv
df = pd.DataFrame.from_records([
{'id': 1, 'var1': 0.1, 'ntv': ntv(0,10), 'E': 1},
{'id':2, 'var1': 0.5, 'ntv': ntv(0,12), 'E': 0}
], index=('ntv', 'id'))
看起来像这样:
E var1
ntv id
(0, 10] 1 1 0.1
(0, 12] 2 0 0.5
我想要做的是以特定值切入DataFrame并返回具有包含该值的间隔的所有行。例如:
df.loc[4]
应该返回(平凡)
E var1
id
1 1 0.1
2 0 0.5
问题是我不断获得关于索引的TypeError
,而docs显示了类似的操作(但是在单级索引上)确实产生了我正在寻找的内容
TypeError: only integer scalar arrays can be converted to a scalar index
我尝试了很多东西,似乎没有什么能正常工作。我可以在数据框中包含id
列,但我宁愿保持我的索引唯一,我会不断地调用set_index('id')
。
我感觉要么a)我错过了关于MultiIndexes的东西,或者b)在MultiIndex中使用IntervalIndex存在错误/歧义。
答案 0 :(得分:6)
由于我们正在发言间隔,因此有一个名为get_loc
的方法来查找间隔之间具有值的行。说出我的意思:
from pandas import Interval as ntv
df = pd.DataFrame.from_records([
{'id': 1, 'var1': 0.1, 'ntv': ntv(0,10), 'E': 1},
{'id':2, 'var1': 0.5, 'ntv': ntv(0,12), 'E': 0}
], index=('ntv', 'id'))
df.iloc[(df.index.get_level_values(0).get_loc(4))]
E var1
ntv id
(0, 10] 1 1 0.1
(0, 12] 2 0 0.5
df.iloc[(df.index.get_level_values(0).get_loc(11))]
E var1
ntv id
(0, 12] 2 0 0.5
如果你有一行的多行数据,这也适用于
df = pd.DataFrame.from_records([
{'id': 1, 'var1': 0.1, 'ntv': ntv(0,10), 'E': 1},
{'id': 3, 'var1': 0.1, 'ntv': ntv(0,10), 'E': 1},
{'id':2, 'var1': 0.5, 'ntv': ntv(0,12), 'E': 0}
], index=('ntv', 'id'))
df.iloc[(df.index.get_level_values(0).get_loc(4))]
E var1
ntv id
(0, 10] 1 1 0.1
3 1 0.1
(0, 12] 2 0 0.5
如果你用列表理解来计算时间,这种方法对于大型数据帧来说更快,即
ndf = pd.concat([df]*10000)
%%timeit
ndf.iloc[ndf.index.get_level_values(0).get_loc(4)]
10 loops, best of 3: 32.8 ms per loop
%%timeit
intervals = ndf.index.get_level_values(0)
mask = [4 in i for i in intervals]
ndf.loc[mask]
1 loop, best of 3: 193 ms per loop
答案 1 :(得分:3)
所以我做了一些挖掘尝试去理解这个问题。如果我尝试运行您的代码,则会发生以下情况。 您尝试使用索引索引到索引标签 “slice(array([0,1],dtype = int64),array([1,2],dtype = int64),None)”
(当我说index_type我指的是Pandas数据类型)
index_type的标签是映射到index_type的levels数组的索引列表。以下是文档中的示例。
+---------------------------------------------------+
| id_no | fullname | gender | dec 1 | dec 2 | dec 3 |
| 1 | stud1 | male | A | P | P |
注意标签中的第二个列表如何连接到级别的顺序。 level [1] [1]等于红色,等级[1] [0]等于蓝色。
无论如何,这就是说我不相信intervalindex意味着以重叠的方式使用。如果你看一下它的原始提案 https://github.com/pandas-dev/pandas/issues/7640
“IntervalIndex将是一个单调且不重叠的一维间隔数组。”
我的建议是将间隔移到一列中。您可以用numba编写一个简单的函数来测试每个区间中是否有数字。你介意解释你从间隔中受益的方式吗?
答案 2 :(得分:2)
躲避@ Dark solution Index.get_loc
,Index.get_indexer
只需拨打idx = df.index.get_level_values(0)
df.iloc[idx.get_indexer([4])]
,因此当你不打电话时,调用基础方法会更有效率。 ; t有其他参数和繁文缛节。
intervals = df.index.get_level_values(0)
mask = [4 in i for i in intervals]
df.loc[mask]
我最初提出的解决方案:
df.reset_index(level=1, drop=True).loc[4] # good
df.loc[4] # TypeError
无论如何,它确实很奇怪,虽然它们会返回两个不同的结果,但看起来它与索引是唯一的/单调的/两者都不相符:
2017-12-03T19:48:51.187449+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/cookies.rb:613:in `call'
2017-12-03T19:48:51.187450+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:97:in `run_callbacks'
2017-12-03T19:48:51.187450+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
2017-12-03T19:48:51.187451+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/callbacks.rb:24:in `call'
2017-12-03T19:48:51.187452+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
2017-12-03T19:48:51.187452+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
2017-12-03T19:48:51.187453+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:24:in `block in call'
2017-12-03T19:48:51.187453+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:36:in `call_app'
2017-12-03T19:48:51.187454+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/tagged_logging.rb:69:in `block in tagged'
2017-12-03T19:48:51.187708+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/tagged_logging.rb:26:in `tagged'
2017-12-03T19:48:51.187710+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/tagged_logging.rb:69:in `tagged'
2017-12-03T19:48:51.187711+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
2017-12-03T19:48:51.187711+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:24:in `call'
2017-12-03T19:48:51.187712+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/request_id.rb:25:in `call'
2017-12-03T19:48:51.187713+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/method_override.rb:22:in `call'
2017-12-03T19:48:51.187717+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
2017-12-03T19:48:51.187714+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/runtime.rb:22:in `call'
2017-12-03T19:48:51.187717+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/executor.rb:12:in `call'
2017-12-03T19:48:51.187718+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/static.rb:125:in `call'
2017-12-03T19:48:51.187719+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/sendfile.rb:111:in `call'
2017-12-03T19:48:51.187719+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/engine.rb:522:in `call'
2017-12-03T19:48:51.187720+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/puma-3.11.0/lib/puma/configuration.rb:225:in `call'
2017-12-03T19:48:51.187721+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/puma-3.11.0/lib/puma/server.rb:624:in `handle_request'
2017-12-03T19:48:51.187721+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/puma-3.11.0/lib/puma/server.rb:438:in `process_client'
2017-12-03T19:48:51.187722+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/puma-3.11.0/lib/puma/server.rb:302:in `block in run'
2017-12-03T19:48:51.187723+00:00 app[web.1]: [076125bd-21fe-447f-98e8-2036caa4a683] vendor/bundle/ruby/2.3.0/gems/puma-3.11.0/lib/puma/thread_pool.rb:120:in `block in spawn_thread'
2017-12-03T20:03:43.644271+00:00 heroku[worker.1]: State changed from crashed to starting
2017-12-03T20:03:46.386095+00:00 heroku[worker.1]: Starting process with command `bundle exec rake jobs:work`
2017-12-03T20:03:46.976688+00:00 heroku[worker.1]: State changed from starting to up
2017-12-03T20:03:49.186838+00:00 app[worker.1]: rake aborted!
2017-12-03T20:03:49.186858+00:00 app[worker.1]: Don't know how to build task 'jobs:work' (see --tasks)
2017-12-03T20:03:49.187090+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.15.2/lib/bundler/cli/exec.rb:74:in `load'
2017-12-03T20:03:49.187091+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.15.2/lib/bundler/cli/exec.rb:74:in `kernel_load'
2017-12-03T20:03:49.187091+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.15.2/lib/bundler/cli/exec.rb:27:in `run'
2017-12-03T20:03:49.187092+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.15.2/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
2017-12-03T20:03:49.187093+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.15.2/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
2017-12-03T20:03:49.187092+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.15.2/lib/bundler/cli.rb:360:in `exec'
2017-12-03T20:03:49.187089+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.3.0/gems/rake-12.3.0/exe/rake:27:in `<top (required)>'
2017-12-03T20:03:49.187094+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.15.2/lib/bundler/vendor/thor/lib/thor/base.rb:444:in `start'
2017-12-03T20:03:49.187095+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.15.2/lib/bundler/cli.rb:10:in `start'
2017-12-03T20:03:49.187095+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.15.2/exe/bundle:30:in `block in <top (required)>'
2017-12-03T20:03:49.187097+00:00 app[worker.1]: /app/bin/bundle:3:in `<main>'
2017-12-03T20:03:49.187094+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.15.2/lib/bundler/cli.rb:20:in `dispatch'
2017-12-03T20:03:49.187099+00:00 app[worker.1]: (See full trace by running task with --trace)
2017-12-03T20:03:49.187096+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.15.2/lib/bundler/friendly_errors.rb:121:in `with_friendly_errors'
2017-12-03T20:03:49.187097+00:00 app[worker.1]: /app/bin/bundle:3:in `load'
2017-12-03T20:03:49.187096+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.15.2/exe/bundle:22:in `<top (required)>'
2017-12-03T20:03:49.187094+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.3.0/gems/bundler-1.15.2/lib/bundler/vendor/thor/lib/thor.rb:369:in `dispatch'
2017-12-03T20:03:49.337634+00:00 heroku[worker.1]: State changed from up to crashed
2017-12-03T20:03:49.326887+00:00 heroku[worker.1]: Process exited with status 1
2017-12-03T20:06:51.940770+00:00 heroku[router]: at=info method=GET path="/" host=mightymixer.herokuapp.com request_id=cf3559e7-f176-4ddd-848e-ae08dede69ce fwd="104.152.232.201" dyno=web.1 connect=2ms service=10ms status=404 bytes=1902 protocol=https
2017-12-03T20:06:51.943218+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] Started GET "/" for 104.152.232.201 at 2017-12-03 20:06:51 +0000
2017-12-03T20:06:51.945172+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce]
2017-12-03T20:06:51.945204+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] ActionController::RoutingError (uninitialized constant PostController):
2017-12-03T20:06:51.945237+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce]
2017-12-03T20:06:51.945371+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/inflector/methods.rb:269:in `const_get'
2017-12-03T20:06:51.945377+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/inflector/methods.rb:267:in `inject'
2017-12-03T20:06:51.945374+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/inflector/methods.rb:269:in `block in constantize'
2017-12-03T20:06:51.945376+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/inflector/methods.rb:267:in `each'
2017-12-03T20:06:51.945378+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/inflector/methods.rb:267:in `constantize'
2017-12-03T20:06:51.945379+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:582:in `get'
2017-12-03T20:06:51.945380+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:613:in `constantize'
2017-12-03T20:06:51.945381+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/http/request.rb:85:in `controller_class_for'
2017-12-03T20:06:51.945381+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/http/request.rb:78:in `controller_class'
2017-12-03T20:06:51.945382+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:43:in `controller'
2017-12-03T20:06:51.945383+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:29:in `serve'
2017-12-03T20:06:51.945384+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/journey/router.rb:50:in `block in serve'
2017-12-03T20:06:51.945385+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/journey/router.rb:33:in `each'
2017-12-03T20:06:51.945385+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/journey/router.rb:33:in `serve'
2017-12-03T20:06:51.945388+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/etag.rb:25:in `call'
2017-12-03T20:06:51.945386+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:834:in `call'
2017-12-03T20:06:51.945391+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/session/abstract/id.rb:232:in `context'
2017-12-03T20:06:51.945389+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/conditional_get.rb:25:in `call'
2017-12-03T20:06:51.945390+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/head.rb:12:in `call'
2017-12-03T20:06:51.945392+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/session/abstract/id.rb:226:in `call'
2017-12-03T20:06:51.945393+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
2017-12-03T20:06:51.945392+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/cookies.rb:613:in `call'
2017-12-03T20:06:51.945394+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:97:in `run_callbacks'
2017-12-03T20:06:51.945395+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/callbacks.rb:24:in `call'
2017-12-03T20:06:51.945396+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
2017-12-03T20:06:51.945397+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:36:in `call_app'
2017-12-03T20:06:51.945395+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
2017-12-03T20:06:51.945398+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:24:in `block in call'
2017-12-03T20:06:51.945398+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/tagged_logging.rb:69:in `block in tagged'
2017-12-03T20:06:51.945410+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/tagged_logging.rb:26:in `tagged'
2017-12-03T20:06:51.945411+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/tagged_logging.rb:69:in `tagged'
2017-12-03T20:06:51.945412+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:24:in `call'
2017-12-03T20:06:51.945412+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
2017-12-03T20:06:51.945413+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/request_id.rb:25:in `call'
2017-12-03T20:06:51.945414+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/method_override.rb:22:in `call'
2017-12-03T20:06:51.945415+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/runtime.rb:22:in `call'
2017-12-03T20:06:51.945417+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
2017-12-03T20:06:51.945417+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/executor.rb:12:in `call'
2017-12-03T20:06:51.945418+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/middleware/static.rb:125:in `call'
2017-12-03T20:06:51.945419+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/sendfile.rb:111:in `call'
2017-12-03T20:06:51.945420+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/puma-3.11.0/lib/puma/configuration.rb:225:in `call'
2017-12-03T20:06:51.945420+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/engine.rb:522:in `call'
2017-12-03T20:06:51.945422+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/puma-3.11.0/lib/puma/server.rb:438:in `process_client'
2017-12-03T20:06:51.945421+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/puma-3.11.0/lib/puma/server.rb:624:in `handle_request'
2017-12-03T20:06:51.945423+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/puma-3.11.0/lib/puma/server.rb:302:in `block in run'
2017-12-03T20:06:51.945424+00:00 app[web.1]: [cf3559e7-f176-4ddd-848e-ae08dede69ce] vendor/bundle/ruby/2.3.0/gems/puma-3.11.0/lib/puma/thread_pool.rb:120:in `block in spawn_thread'
答案 3 :(得分:2)
这不是一个真正的解决方案,我不完全理解,但认为它可能与你的间隔指数不单调(因为你有重叠的间隔)。我想在某种意义上可以认为是单调的,所以也许你可以说重叠意味着指数不是唯一的?
无论如何,看看这个github问题:
ENH: Implement MultiIndex.is_monotonic_decreasing #17455
以下是您的数据示例,但将间隔更改为非重叠(0,6)&amp; (7,12):
df = pd.DataFrame.from_records([
{'id': 1, 'var1': 0.1, 'ntv': ntv(0, 6), 'E': 1},
{'id': 2, 'var1': 0.5, 'ntv': ntv(7,12), 'E': 0}
], index=('ntv', 'id'))
现在,loc
正常工作:
df.loc[4]
E var1
id
1 1 0.1
答案 4 :(得分:0)
def check_value(num):
return df[[num in i for i in map(lambda x: x[0], df.index)]]
a = check_value(4)
a
>>
E var1
ntv id
(0, 10] 1 1 0.1
(0, 12] 2 0 0.5
如果要删除索引级别,可以添加
a.index = a.droplevel(0)