collapse a pandas MultiIndex

Question:

Suppose I have a DataFrame with MultiIndex columns. How can I collapse the levels to a concatenation of the values so that I only have one level?

Setup

np.random.seed([3, 14])
col = pd.MultiIndex.from_product([list('ABC'), list('DE'), list('FG')])
df = pd.DataFrame(np.random.rand(4, 12) * 10, columns=col).astype(int)

print df

   A           B           C         
   D     E     D     E     D     E   
   F  G  F  G  F  G  F  G  F  G  F  G
0  2  1  1  7  5  9  9  2  7  4  0  3
1  3  7  1  1  5  3  1  4  3  5  6  0
2  2  6  9  9  9  5  7  0  1  2  7  5
3  2  2  8  0  3  9  4  7  0  8  2  5

I want the result to look like this:

   ADF  ADG  AEF  AEG  BDF  BDG  BEF  BEG  CDF  CDG  CEF  CEG
0    2    1    1    7    5    9    9    2    7    4    0    3
1    3    7    1    1    5    3    1    4    3    5    6    0
2    2    6    9    9    9    5    7    0    1    2    7    5
3    2    2    8    0    3    9    4    7    0    8    2    5
Asked By: piRSquared

||

Answers:

you may try this:

In [200]: cols = pd.Series(df.columns.tolist()).apply(pd.Series).sum(axis=1)

In [201]: cols
Out[201]:
0     ADF
1     ADG
2     AEF
3     AEG
4     BDF
5     BDG
6     BEF
7     BEG
8     CDF
9     CDG
10    CEF
11    CEG
dtype: object

Solution

I did this

def collapse_columns(df):
    df = df.copy()
    if isinstance(df.columns, pd.MultiIndex):
        df.columns = df.columns.to_series().apply(lambda x: "".join(x))
    return df

I had to check if its a MultiIndex because if it wasn’t, I’d split a string and recombine it with what ever separator I chose in the join.

Answered By: piRSquared
df.columns = df.columns.to_series().apply(''.join)

This will give no separation, but you can sub in ‘_’ for ” or any other separator you might want.

Answered By: Pat Kenny

Solution 1)

df.columns = df.columns.to_series().str.join('_')
print(df.columns.shape) #(1,_X_) # a 2 D Array.

OR BETTER Solution 2

pivoteCols = df.columns.to_series().str.join('_')
pivoteCols = pivoteCols.values.reshape(len(pivoteCols))
df.columns = pivoteCols
print(df.columns.shape) # One Dimensional 
Answered By: user2458922
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.