我使用 to_flat_index() 来展平列,最后得到了像 ('Method', 'sum')
这样的列名,我试图从中删除特殊字符。但是当我尝试删除它们时,它会将所有列名称更改为 nan
函数尝试:
df_pred.columns = df_pred.columns.str.replace("[(,),']", '')
预期结果:MethodSum
答案 0 :(得分:1)
您的列似乎是多索引的,因为您使用了 to_flat_index
。
>>> df
bar baz foo qux
one two one two one two one two
0 0.713825 0.015553 0.036683 0.388443 0.729509 0.699883 0.125998 0.407517
1 0.820843 0.259039 0.217209 0.021479 0.845530 0.112166 0.219814 0.527205
2 0.734660 0.931206 0.651559 0.337565 0.422514 0.873403 0.979258 0.269594
3 0.314323 0.857317 0.222574 0.811631 0.313495 0.315072 0.354784 0.394564
4 0.672068 0.658103 0.402914 0.430545 0.879331 0.015605 0.086048 0.918678
试试:
>>> df.columns.to_flat_index().map(''.join)
Index(['barone', 'bartwo', 'bazone', 'baztwo',
'fooone', 'footwo', 'quxone', 'quxtwo'],
dtype='object')