Pandas combining rows with different row Names [closed]

流过昼夜 提交于 2019-12-12 04:18:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!