Make a Pandas MultiIndex from a product of iterables?

前端 未结 1 1509
粉色の甜心
粉色の甜心 2021-02-05 17:13

I have a utility function for creating a Pandas MultiIndex when I have two or more iterables and I want an index key for each unique pairing of the values in those iterables. It

相关标签:
1条回答
  • 2021-02-05 18:06

    This is a very similar construction (but using cartesian_product which for larger arrays is faster than itertools.product)

    In [2]: from pandas.tools.util import cartesian_product
    
    In [3]: MultiIndex.from_arrays(cartesian_product([range(3),list('ab')]))
    Out[3]: 
    MultiIndex(levels=[[0, 1, 2], [u'a', u'b']],
               labels=[[0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1]])
    

    could be added as a convience method, maybe MultiIndex.from_iterables(...)

    pls open an issue (and PR if you'd like)

    FYI I very rarely actually construct a multi-index 'manually', almost always easier to actually construct a frame and just set_index.

    In [10]: df = DataFrame(dict(A = np.arange(6), 
                                 B = ['foo'] * 3 + ['bar'] * 3, 
                                 C = np.ones(6)+np.arange(6)%2)
                           ).set_index(['C','B']).sortlevel()
    
    In [11]: df
    Out[11]: 
           A
    C B     
    1 bar  4
      foo  0
      foo  2
    2 bar  3
      bar  5
      foo  1
    
    [6 rows x 1 columns]
    
    0 讨论(0)
提交回复
热议问题