问题
I have following pandas table:
A1 A2 A3 A4 B1 B2 B3 B4
3 0.202425 0.13495 0.202425 0.202425 0.94465 0.877175 0.877175 0.8097
I would like to arrange this table:
A1 A2 A3 A4
0.202425 0.13495 0.202425 0.202425
0.94465 0.877175 0.877175 0.8097
回答1:
You could select the two groups of columns
, rename the second group to match the first group, and then use pd.concat()
to combine vertically:
a_cols = [c for c in df.columns if c.startswith('A')]
b_cols = [c for c in df.columns if c not in a_cols]
col_dict = dict(zip(b_cols, a_cols))
pd.concat([df.loc[:, a_cols], df.loc[:, b_cols].rename(columns=col_dict)])
A1 A2 A3 A4
0 0.202425 0.134950 0.202425 0.202425
0 0.944650 0.877175 0.877175 0.809700
来源:https://stackoverflow.com/questions/34600342/pandas-combining-rows-with-different-row-names