I have the initial dataframe :
r_id1 r_score1 rid2 r_score2
Rank
ID1 ID2
1 A-1 id-1 1.23 id-34 6.78
2 A-1 id-9 2.34 id-45 3.45
3 A-2 id-8 3.56 id-32 4.56
4 A-3 id-6 4.35 id-10 3.98
5 A-4 id-4 7.89 id-67 2.98
I want my data frame to be (Result_df):
Score_R1 Score_R2
r_id1 r_score1 rid2 r_score2
ID1 ID2
1 A-1 id-1 1.23 id-34 6.78
2 A-1 id-9 2.34 id-45 3.45
3 A-2 id-8 3.56 id-32 4.56
4 A-3 id-6 4.35 id-10 3.98
5 A-4 id-4 7.89 id-67 2.98
My dataframe is multiindex and with MultiIndex columns. I tried this piece of code
final_df.columns = [' '.join(col).strip() for col in final_df.columns.values]
which gives me this output
ID1 ID2 r_id1 r_score1 rid2 r_score2
1 A-1 id-1 1.23 id-34 6.78
2 A-1 id-9 2.34 id-45 3.45
3 A-2 id-8 3.56 id-32 4.56
4 A-3 id-6 4.35 id-10 3.98
5 A-4 id-4 7.89 id-67 2.98
After :
cols = final_df.columns.map(''.join)
lvl = 'Score_R' + cols.str.extract('(\d+)', expand=False)
final_df.columns = [lvl, cols]
final_df.to_csv("f.csv")
Output is:
Score_R1 Score_R1 Score_R2 Score_R2
r_id1 r_score1 rid2 r_score2
ID1 ID2
1 A-1 id-1 1.23 id-34 6.78
2 A-1 id-9 2.34 id-45 3.45
3 A-2 id-8 3.56 id-32 4.56
4 A-3 id-6 4.35 id-10 3.98
5 A-4 id-4 7.89 id-67 2.98
I need to combine the column headers with same name
Score_R1 Score_R2
r_id1 r_score1 rid2 r_score2
ID1 ID2
1 A-1 id-1 1.23 id-34 6.78
2 A-1 id-9 2.34 id-45 3.45