理解pd.MultiIndex.from_product

  • classmethod MultiIndex.from_product(iterables, sortorder=None,names=)

    Make a MultiIndex from the cartesian product of multiple iterables.

  • Parameters

    1. iterable : list/sequence of iterables

      Each iterable has unique for each level of the index

    2. sortorder : int or None

      Level of sortedness (must be lexicographically sorted by that level)

    3. names : list / sequence of str, optional

      Names for the levels in the index.

      Changed in version 1.0.0 : If not explicitly provided, names will be inferred from the elments of itrables if an element has a name attribute

  • Example

    >>> numbers = [1,2,3]
    >>> colors = ['green','purple']
    >>> pd.MultiIndex.from_product([numbers, colors], names=['number','color'])
    MultiIndex(levels=[[1, 2, 3], ['green', 'purple']],
               labels=[[0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1]],
               names=['number', 'color'])
    
  • cartesian product

    For two sets A and B, the Cartesian product of A and B is denoted by A × B A\times B A×B and defined as :
    A × B = ( a , b ) ∣ a ∈ A    a n d    b ∈ B A\times B={(a,b)|a\in A \; and\;b\in B} A×B=(a,b)aAandbB
    Cartesian Product is the multiplication of two sets to form the set of all ordered pairs.

    The first element of the orderd pair belong to first set and second pair belong the second set.

  • 你可能感兴趣的:(#,小白学Python,MultiIndex,from_product,笛卡尔积)