如何将MultiIndex转换为类型字符串

时间:2016-08-23 22:00:11

标签: python pandas multi-index

考虑MultiIndex idx

idx = pd.MultiIndex.from_product([range(2013, 2016), range(1, 5)])

当我这样做时

idx.to_series().str.join(' ')

我得到了

2013  1   NaN
      2   NaN
      3   NaN
      4   NaN
2014  1   NaN
      2   NaN
      3   NaN
      4   NaN
2015  1   NaN
      2   NaN
      3   NaN
      4   NaN
dtype: float64

这是因为不同级别的dtypes是int而不是strjoin需要str。如何将整个idx转换为str

我已经完成了

join = lambda x, delim=' ': delim.join([str(y) for y in x])
idx.to_series().apply(join, delim=' ')

2013  1    2013 1
      2    2013 2
      3    2013 3
      4    2013 4
2014  1    2014 1
      2    2014 2
      3    2014 3
      4    2014 4
2015  1    2015 1
      2    2015 2
      3    2015 3
      4    2015 4
dtype: object

我希望有一种更简单的方法可以忽略。

4 个答案:

答案 0 :(得分:4)

这样的东西?

idx.to_series().apply(lambda x: '{0}-{1}'.format(*x))

答案 1 :(得分:3)

我不确定这是最优雅的方式,但它应该有效:

idx.get_level_values(0).astype(str).values + ' ' + idx.get_level_values(1).astype(str).values

答案 2 :(得分:1)

最快的是列表理解:

print (['{} {}'.format(i[1], i[0]) for i in idx])
print ([' '.join((str(i[0]), str(i[1]))) for i in idx])

<强>计时

In [21]: %timeit (['{} {}'.format(i[1], i[0]) for i in idx])
The slowest run took 4.68 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 7.51 µs per loop

In [22]: %timeit ([' '.join((str(i[0]), str(i[1]))) for i in idx])
The slowest run took 6.48 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 9.62 µs per loop

In [23]: %timeit (idx.get_level_values(0).astype(str).values + ' ' + idx.get_level_values(1).astype(str).values)
The slowest run took 5.91 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 215 µs per loop

In [24]: %timeit idx.to_series().apply(lambda x: '{0}-{1}'.format(*x))
The slowest run took 5.43 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 369 µs per loop

In [25]: %timeit idx.to_series().str.join(' ')
The slowest run took 5.53 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 394 µs per loop

答案 3 :(得分:0)

使用starmap

itertools的一般解决方案
from itertools import starmap

def flat2(midx, sep=''):
    fstr = sep.join(['{}'] * midx.nlevels)
    return pd.Index(starmap(fstr.format, midx))

示范

midx = pd.MultiIndex.from_product([[1, 2], [3, 4]])
flat(midx)
Index([u'13', u'14', u'23', u'24'], dtype='object')
flat(midx, '_')
Index([u'1_3', u'1_4', u'2_3', u'2_4'], dtype='object')