0

Right now I have a dataframe named result with a multiindex that looks like the below:

col1   col2   bad1   bad2  col5
              col3  col4     

value   value value  value  value

I would like to merge the first two columns and have been doing so like this:

result.columns = [' '.join(col).strip() for col in result.columns.values]

The above however returns this

col1   col2   bad1 col3   bad2 col4  col5
value  value  value       value      value 

I would like the ability to pick which index value when I want so it would look more like this

col1   col2   col3    bad2    col5
value  value  value   value   value 

So in the final result, i chose col3 and bad2 to be the column headers for the 3rd and 4th column.

skim8201
  • 81
  • 4
  • Try direct indexing: result.columns = [col[0].strip() for col in result.columns.values] # In this example I used index 0. – Sheikh Arbaz Oct 23 '18 at 07:32
  • Please read [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/8881141), then edit your question accordingly. – Mr. T Oct 23 '18 at 10:08

0 Answers0