pandas 数据框行多索引跳过一个
pandas dataframe row multiindex skip one
在我的数据框中选择第一个和第三个子索引时,我无法跳过(包括所有)一个子索引:
我有一个数据框(测试),形式如下:
signal dat1 dat2 dat3
condition epoch time
A 0 -1100 1.001322 2.884899 -0.659933
-1099 1.081918 3.389470 -0.413069
-1098 1.168483 3.585312 -0.277902
-1097 1.237908 3.439242 -0.299783
-1096 1.263452 2.942262 -0.496889
A 1 -1100 1.001322 2.884899 -0.659933
-1099 1.081918 3.389470 -0.413069
-1098 1.168483 3.585312 -0.277902
-1097 1.237908 3.439242 -0.299783
-1096 1.263452 2.942262 -0.496889
我想提取条件和时间点,所以结果 table 看起来像:
signal dat1 dat2 dat3
condition epoch time
A 0 -1000 1.001322 2.884899 -0.659933
-999 1.081918 3.389470 -0.413069
-998 1.168483 3.585312 -0.277902
-997 1.237908 3.439242 -0.299783
-996 1.263452 2.942262 -0.496889
A 1 -1000 1.001322 2.884899 -0.659933
-999 1.081918 3.389470 -0.413069
-998 1.168483 3.585312 -0.277902
-997 1.237908 3.439242 -0.299783
-996 1.263452 2.942262 -0.496889
我是 pandas 的新人,我尝试了很多东西。
我认为解决方案是:
test.loc['A',:,[-1000:-50]]
如果我 select 时间点之间的间隔小于大约 50 个数据点,它实际上会起作用;然而,对于 more,第一个索引被忽略,它 returns 来自第一个索引(-1100)的所有值。这真的让我很奇怪。所以它看起来像这样(很好):
In [284]: test.loc['A',:,-1000:-950].head()
Out[284]:
signal dat1 dat2 dat3
condition epoch time
A 0 -1000 0.776851 -0.591070 0.435884
-999 0.908675 -1.042335 0.084967
-998 0.942239 -1.583269 -0.266314
-997 0.901392 -2.146548 -0.602187
-996 0.814778 -2.663253 -0.892899
但随后发生了这种情况:
test.loc['A',:,-1000:-900].head()
Out[285]:
signal dat1 dat2 dat3
condition epoch time
A 0 -1100 1.001322 2.884899 -0.659933
-1099 1.081918 3.389470 -0.413069
-1098 1.168483 3.585312 -0.277902
-1097 1.237908 3.439242 -0.299783
-1096 1.263452 2.942262 -0.496889
我做错了什么,或者是否有其他简单/直观的索引(我尝试了一些 .ix、slice(),但 none 成功)到 select 所有时期但限制时间?
这个有效:
import pandas as pd
import numpy as np
np.random.seed(0)
idx = pd.IndexSlice
midx = pd.MultiIndex.from_product([['A', 'B'], [0, 1], range(-1000, 0)])
df = pd.DataFrame(np.random.randn(4000, 3), columns=['dat1', 'dat2', 'dat3'], index=midx)
df.sort_index(inplace=True)
>>> df.loc[idx['A', :, -1000:-950], :].head()
dat1 dat2 dat3
A 0 -1000 1.764052 0.400157 0.978738
-999 2.240893 1.867558 -0.977278
-998 0.950088 -0.151357 -0.103219
-997 0.410599 0.144044 1.454274
-996 0.761038 0.121675 0.443863
重现此问题(可能是错误)。注意在第二个头数据帧中时间如何从 -1100 开始,尽管切片从 -1000 开始:
np.random.seed(0)
midx = pd.MultiIndex.from_product([['CS'], range(20), range(-1100, 6000)])
df = pd.DataFrame(np.random.randn(7100*20, 3), columns=['dat1', 'dat2', 'dat3'], index=midx)
>>> df.loc[idx['CS', :, -1000:-950], :].head()
dat1 dat2 dat3
CS 0 -1000 -1.306527 1.658131 -0.118164
-999 -0.680178 0.666383 -0.460720
-998 -1.334258 -1.346718 0.693773
-997 -0.159573 -0.133702 1.077744
-996 -1.126826 -0.730678 -0.384880
>>> df.loc[idx['CS', :, -1000:-50], :].head()
dat1 dat2 dat3
CS 0 -1100 1.764052 0.400157 0.978738 # <<< Index Level 2 should start at -1000
-1099 2.240893 1.867558 -0.977278
-1098 0.950088 -0.151357 -0.103219
-1097 0.410599 0.144044 1.454274
-1096 0.761038 0.121675 0.443863
这是使用 Python 3.5.1 |Continuum Analytics, Inc.| (默认值,2015 年 12 月 7 日,11:24:55)和 Pandas 0.18.0.
由错误引起;更新过去 this 修复或 pandas 0.18.2
在我的数据框中选择第一个和第三个子索引时,我无法跳过(包括所有)一个子索引:
我有一个数据框(测试),形式如下:
signal dat1 dat2 dat3
condition epoch time
A 0 -1100 1.001322 2.884899 -0.659933
-1099 1.081918 3.389470 -0.413069
-1098 1.168483 3.585312 -0.277902
-1097 1.237908 3.439242 -0.299783
-1096 1.263452 2.942262 -0.496889
A 1 -1100 1.001322 2.884899 -0.659933
-1099 1.081918 3.389470 -0.413069
-1098 1.168483 3.585312 -0.277902
-1097 1.237908 3.439242 -0.299783
-1096 1.263452 2.942262 -0.496889
我想提取条件和时间点,所以结果 table 看起来像:
signal dat1 dat2 dat3
condition epoch time
A 0 -1000 1.001322 2.884899 -0.659933
-999 1.081918 3.389470 -0.413069
-998 1.168483 3.585312 -0.277902
-997 1.237908 3.439242 -0.299783
-996 1.263452 2.942262 -0.496889
A 1 -1000 1.001322 2.884899 -0.659933
-999 1.081918 3.389470 -0.413069
-998 1.168483 3.585312 -0.277902
-997 1.237908 3.439242 -0.299783
-996 1.263452 2.942262 -0.496889
我是 pandas 的新人,我尝试了很多东西。
我认为解决方案是:
test.loc['A',:,[-1000:-50]]
如果我 select 时间点之间的间隔小于大约 50 个数据点,它实际上会起作用;然而,对于 more,第一个索引被忽略,它 returns 来自第一个索引(-1100)的所有值。这真的让我很奇怪。所以它看起来像这样(很好):
In [284]: test.loc['A',:,-1000:-950].head()
Out[284]:
signal dat1 dat2 dat3
condition epoch time
A 0 -1000 0.776851 -0.591070 0.435884
-999 0.908675 -1.042335 0.084967
-998 0.942239 -1.583269 -0.266314
-997 0.901392 -2.146548 -0.602187
-996 0.814778 -2.663253 -0.892899
但随后发生了这种情况:
test.loc['A',:,-1000:-900].head()
Out[285]:
signal dat1 dat2 dat3
condition epoch time
A 0 -1100 1.001322 2.884899 -0.659933
-1099 1.081918 3.389470 -0.413069
-1098 1.168483 3.585312 -0.277902
-1097 1.237908 3.439242 -0.299783
-1096 1.263452 2.942262 -0.496889
我做错了什么,或者是否有其他简单/直观的索引(我尝试了一些 .ix、slice(),但 none 成功)到 select 所有时期但限制时间?
这个有效:
import pandas as pd
import numpy as np
np.random.seed(0)
idx = pd.IndexSlice
midx = pd.MultiIndex.from_product([['A', 'B'], [0, 1], range(-1000, 0)])
df = pd.DataFrame(np.random.randn(4000, 3), columns=['dat1', 'dat2', 'dat3'], index=midx)
df.sort_index(inplace=True)
>>> df.loc[idx['A', :, -1000:-950], :].head()
dat1 dat2 dat3
A 0 -1000 1.764052 0.400157 0.978738
-999 2.240893 1.867558 -0.977278
-998 0.950088 -0.151357 -0.103219
-997 0.410599 0.144044 1.454274
-996 0.761038 0.121675 0.443863
重现此问题(可能是错误)。注意在第二个头数据帧中时间如何从 -1100 开始,尽管切片从 -1000 开始:
np.random.seed(0)
midx = pd.MultiIndex.from_product([['CS'], range(20), range(-1100, 6000)])
df = pd.DataFrame(np.random.randn(7100*20, 3), columns=['dat1', 'dat2', 'dat3'], index=midx)
>>> df.loc[idx['CS', :, -1000:-950], :].head()
dat1 dat2 dat3
CS 0 -1000 -1.306527 1.658131 -0.118164
-999 -0.680178 0.666383 -0.460720
-998 -1.334258 -1.346718 0.693773
-997 -0.159573 -0.133702 1.077744
-996 -1.126826 -0.730678 -0.384880
>>> df.loc[idx['CS', :, -1000:-50], :].head()
dat1 dat2 dat3
CS 0 -1100 1.764052 0.400157 0.978738 # <<< Index Level 2 should start at -1000
-1099 2.240893 1.867558 -0.977278
-1098 0.950088 -0.151357 -0.103219
-1097 0.410599 0.144044 1.454274
-1096 0.761038 0.121675 0.443863
这是使用 Python 3.5.1 |Continuum Analytics, Inc.| (默认值,2015 年 12 月 7 日,11:24:55)和 Pandas 0.18.0.
由错误引起;更新过去 this 修复或 pandas 0.18.2