1 回答
TA贡献1824条经验 获得超6个赞
您可以用于str.extract
从列名称中获取数字,添加前缀并最后将原始列分配回MultiIndex in columns
:
print (df.columns.tolist())
[('r_id1', ''), ('r_score1', ''), ('rid2', ''), ('r_score2', '')]
cols = df.columns.map(''.join)
print (cols.tolist())
['r_id1', 'r_score1', 'rid2', 'r_score2']
lvl = 'Score_R' + cols.str.extract('(\d+)', expand=False)
print (lvl)
Index(['Score_R1', 'Score_R1', 'Score_R2', 'Score_R2'], dtype='object')
df.columns = [lvl, cols]
print (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
df.columns = df.columns.map('_'.join)
print (df)
Score_R1_r_id1 Score_R1_r_score1 Score_R2_rid2 Score_R2_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
编辑:您可以将第一级的缺失值替换为空字符串:
cols = df.columns.droplevel(-1)
lvl = 'Score_R' + cols.str.extract('(\d+)', expand=False)
print (lvl)
Index(['Score_R1', 'Score_R1', 'Score_R2', 'Score_R2'], dtype='object')
lvl = lvl.where(~lvl.duplicated(), '')
print (lvl)
Index(['Score_R1', '', 'Score_R2', ''], dtype='object')
df.columns = [lvl, cols]
print (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
print (df.columns)
MultiIndex([('Score_R1', 'r_id1'),
( '', 'r_score1'),
('Score_R2', 'rid2'),
( '', 'r_score2')],
)
添加回答
举报