Use DataFrame.set_index
with DataFrame.stack
for MultiIndex Series
, convert to one column DataFrame by Series.to_frame
and transpose for one row DataFrame
, last flatten MultiIndex
by join
:
df1 = s.set_index('Gender').stack().to_frame().T
df1.columns = df1.columns.map(lambda x: f'{x[0]} {x[1]}')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…