pandas.MultiIndex.from_tuples#

类方法 MultiIndex.from_tuples(tuples, sortorder=None, names=None)[源代码]#

将元组列表转换为 MultiIndex。

参数:
tuples列表 / 元组序列

每个元组代表一行/一列的索引。

sortorderint or None

排序级别(必须按该级别按字典顺序排序)。

nameslist / sequence of str, optional

索引中级别的名称。

返回:
MultiIndex

另请参阅

MultiIndex.from_arrays

将数组列表转换为 MultiIndex。

MultiIndex.from_product

从可迭代对象的笛卡尔积创建 MultiIndex。

MultiIndex.from_frame

从 DataFrame 创建 MultiIndex。

示例

>>> tuples = [(1, "red"), (1, "blue"), (2, "red"), (2, "blue")]
>>> pd.MultiIndex.from_tuples(tuples, names=("number", "color"))
MultiIndex([(1,  'red'),
            (1, 'blue'),
            (2,  'red'),
            (2, 'blue')],
           names=['number', 'color'])