Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a MultiIndex to type string

consider the MultiIndex idx

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

When I 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 the dtypes of the different levels are int and not str. join expects a str. How do I convert the whole idx to str?

I've done

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

I expect there is a simpler way that I'm overlooking.

like image 794
piRSquared Avatar asked Aug 23 '16 22:08

piRSquared


People also ask

How do I convert MultiIndex to single index in pandas?

Output: Now, the dataframe has Hierarchical Indexing or multi-indexing. To revert the index of the dataframe from multi-index to a single index using the Pandas inbuilt function reset_index(). Returns: (Data Frame or None) DataFrame with the new index or None if inplace=True.

How convert MultiIndex to columns in pandas?

pandas MultiIndex to ColumnsUse pandas DataFrame. reset_index() function to convert/transfer MultiIndex (multi-level index) indexes to columns. The default setting for the parameter is drop=False which will keep the index values as columns and set the new index to DataFrame starting from zero.

What is MultiIndex?

A MultiIndex, also known as a multi-level index or hierarchical index, allows you to have multiple columns acting as a row identifier, while having each index column related to another through a parent/child relationship.


2 Answers

Something like this?

idx.to_series().apply(lambda x: '{0}-{1}'.format(*x))
like image 191
bananafish Avatar answered Sep 19 '22 01:09

bananafish


I'm not sure it's the most elegant way, but it should work:

idx.get_level_values(0).astype(str).values + ' ' + idx.get_level_values(1).astype(str).values
like image 32
MaxU - stop WAR against UA Avatar answered Sep 21 '22 01:09

MaxU - stop WAR against UA