python - How do I convert a MultiIndex to type string -


consider multiindex idx

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

when do

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

i get

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 

this happens because dtypes of different levels int , not str. join expects str. how convert whole idx str?

i've done

join = lambda x, delim=' ': delim.join([str(y) 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 

i expect there simpler way i'm overlooking.

i'm not sure it's elegant way, should work:

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

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -