多索引索引的顺序有什么影响?
What impact does the order of a multiindex index have?
SO 上有很多关于如何重新排序多索引索引的问题。
该订单在性能或其他方面是否有任何 material 差异?
为什么有人会对索引重新排序(而不是为了展示)?
在对某些分类变量的数据帧进行子集化时,有序索引与 loc
一起很有用。例如,对于以下数据框
import pandas as pd
index = ['a', 'b', 'c', 'a', 'b', 'c']
x = np.arange(6)
df = pd.DataFrame({"x": x},
index = pd.Categorical(index, categories=['a', 'b', 'c'], ordered=True))
df.loc['b':'c']
给出错误,而
df = df.sort_index()
df.loc['b':'c']
给出正确选择的行。
x
b 1
b 4
c 2
c 5
SO 上有很多关于如何重新排序多索引索引的问题。 该订单在性能或其他方面是否有任何 material 差异?
为什么有人会对索引重新排序(而不是为了展示)?
在对某些分类变量的数据帧进行子集化时,有序索引与 loc
一起很有用。例如,对于以下数据框
import pandas as pd
index = ['a', 'b', 'c', 'a', 'b', 'c']
x = np.arange(6)
df = pd.DataFrame({"x": x},
index = pd.Categorical(index, categories=['a', 'b', 'c'], ordered=True))
df.loc['b':'c']
给出错误,而
df = df.sort_index()
df.loc['b':'c']
给出正确选择的行。
x
b 1
b 4
c 2
c 5