- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些带有 MultiIndex 的数据(一些计时统计数据,带有“设备”、“构建配置”、“测试功能”等的索引级别)。我想切出其中一些索引列。
似乎 .loc 函数的“切片器”可能是要走的路。然而docs包含此警告:
Warning: You will need to make sure that the selection axes are fully lexsorted!
you are responsible for ensuring that things are properly sorted
The MultiIndex object has code to explicity check the sort depth. Thus, if you try to index at a depth at which the index is not sorted, it will raise an exception.
sortlevel()
但随后包含以下警告:
There is an important new method sortlevel to sort an axis within a MultiIndex so that its labels are grouped and sorted by the original ordering of the associated factor at that level. Note that this does not necessarily mean the labels will be sorted lexicographically!
In [1]:
import pandas as pd
df = pd.DataFrame({'col1': ['b','d','b','a'], 'col2': [3,1,1,2],
'data':['one','two','three','four']})
df
Out[1]:
col1 col2 data
0 b 3 one
1 d 1 two
2 b 1 three
3 a 2 four
In [2]:
df2 = df.set_index(['col1','col2'])
df2
Out[2]:
data
col1 col2
b 3 one
d 1 two
b 1 three
a 2 four
In [3]: df2.index
Out[3]:
MultiIndex(levels=[[u'a', u'b', u'd'], [1, 2, 3]],
labels=[[1, 2, 1, 0], [2, 0, 0, 1]],
names=[u'col1', u'col2'])
In [4]: df2.index.is_lexsorted()
Out[4]: False
In [5]:
sorted = df2.sortlevel()
sorted
Out[5]:
data
col1 col2
a 2 four
b 1 three
3 one
d 1 two
In [6]: sorted.index.is_lexsorted()
Out[6]: True
In [7]:
df3 = df2
df3.index.set_levels(['b','d','a'], level='col1', inplace=True)
df3.index.set_labels([0,1,0,2], level='col1', inplace=True)
df3
Out[7]:
data
col1 col2
b 3 one
d 1 two
b 1 three
a 2 four
In [8]:
sorted2 = df3.sortlevel()
sorted2
Out[8]:
data
col1 col2
b 1 three
3 one
d 1 two
a 2 four
In [9]: sorted2.index.is_lexsorted()
Out[9]: True
In [10]: sorted2.index
Out[10]:
MultiIndex(levels=[[u'b', u'd', u'a'], [1, 2, 3]],
labels=[[0, 0, 1, 2], [0, 2, 0, 1]],
names=[u'col1', u'col2'])
最佳答案
至于排序,正如@EdChum 指出的,文档 here似乎表明它是按字典顺序排序的。
为了检查您的索引(或列)是否已排序,他们有一个方法 is_lexsorted()
和一个属性 lexsort_depth
(出于某种原因,您无法在文档本身中真正找到)。
例子:
创建一个随机顺序的系列
In [1]:
import pandas as pd
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', '1', '3', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
import random; random.shuffle(tuples)
s = pd.Series(np.random.randn(8), index=pd.MultiIndex.from_tuples(tuples))
s
Out[1]:
baz 3 -0.191653
qux two -1.410311
bar one -0.336475
qux one -1.192908
foo two 0.486401
baz 1 0.888314
foo one -1.504816
bar two 0.917460
dtype: float64
In [2]: s.index.is_lexsorted()
Out[2]: False
In [3]: s.index.lexsort_depth
Out[3]: 0
In [4]: s = s.sortlevel(0, sort_remaining=True)
s
Out[4]:
bar one -0.336475
two 0.917460
baz 1 0.888314
3 -0.191653
foo one -1.504816
two 0.486401
qux one -1.192908
two -1.410311
dtype: float64
In [5]: s.index.is_lexsorted()
Out[5]: True
In [6]: s.index.lexsort_depth
Out[6]: 2
关于pandas - 确保 Pandas MultiIndex 中的字典序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31427466/
我有一个包含 4 个级别的 MultiIndex 的 pandas 数据框。我正在尝试为每个 1 级索引选择具有不同 4 级索引的行。 例子: In [68]: df = pd.DataFrame({
我有一个数据框,其中包含 Year 和 Month 的多个索引,如下所示 | |Value Year |Month| | 1 | 3 1992 | 2 | 5
我有一个带有 3 级 MultiIndex 的 pandas 数据框。我正在尝试根据对应于两个级别的值列表提取此数据帧的行。 我有这样的事情: ix = pd.MultiIndex.from_prod
我想将具有多索引的数据帧中的所有列除以另一个具有多索引的数据帧,更小一级。前两个水平的蛾指数是相同的。并且应该播放第三级。 df_0 = pd.DataFrame( { "col0": [ 1
我想知道是否有人可以帮助我解决这个问题。 如果我有一个简单的数据框: one two three four 0 A 1 a 1 1 A 2 b
我希望创建一个新的 DataFrame,与基于芯片的设备 A 和 B 的结果相对应。 以下是我创建 DataFrame 的代码: import numpy as np import pandas as
我希望创建一个新的 DataFrame,与基于芯片的设备 A 和 B 的结果相对应。 以下是我创建 DataFrame 的代码: import numpy as np import pandas as
请让我知道如何将两个 DataFrame 与排序的 MultiIndexes 连接起来,以便结果具有排序的 MultiIndex。 由于两者都是排序的,算法必须根据两个 DataFrame 中的总行数
基本场景 对于推荐服务,我正在针对一组用户-项目交互训练矩阵分解模型 (LightFM)。为了使矩阵分解模型产生最佳结果,我需要将我的用户 ID 和商品 ID 映射到从 0 开始的连续整数 ID 范围
好的,所以我有一个数据框,其中包含时间序列数据,每列都有一个多行索引。这是数据的样例,它是 csv 格式的。加载数据在这里不是问题。 我想要做的是能够创建一个箱线图,其中包含根据多索引特定行中的不同类
我有这个数据框: df = pd.DataFrame({'NUMBER_1': {('2019-07', 'A'): 4, ('2019-07', 'D'): 2, ('2019-08', 'A'):
我有点难以对用于我的一个数据集的列的 pandas MultiIndex 进行排序: MultiIndex(levels=[['Jan', 'Feb', 'Mar', 'Apr', 'May', 'J
我有点难以对用于我的一个数据集的列的 pandas MultiIndex 进行排序: MultiIndex(levels=[['Jan', 'Feb', 'Mar', 'Apr', 'May', 'J
我有一个列表列表,我想将其制成多索引 Pandas 数据框,然后可以合并到原始 Pandas 数据框。列表的每一行都包含一个观察值,列表中的值由与该观察值匹配的相应行组成。 这是我所拥有的一个简单版本
我有以下 DataFrame(从我无法更改的 CSV 文件中读取): df = pd.DataFrame([['low', 0.5, 123, 0.8, 123],
我正在创建一个 MultiIndex.from_product(),但它必须是来自两个单独的 MultiIndexes 的唯一值的乘积。我下面的解决方案有效,但我想知道是否有更优雅的解决方案。 fro
我有一个很大的pd.DataFrame,a,看起来像: bid TIT IM Equity HELN SE Equity FHZN SE Equity GLEN LN Equity f
我创建了一个带有多索引的数据框,其中一个索引是一个元组。我无法使用 .loc 进行索引。 这是数据框: a = [['a','a','a','b','b','b','c','c'],[('one',2
我有以下数据框 A B C D E F Algo T X
我在使用 MultiIndex 和 stack() 时遇到问题。以下示例基于 solution from Calvin Cheung在 StackOvervlow 上。 === multi.csv =
我是一名优秀的程序员,十分优秀!