Skip to content Skip to sidebar Skip to footer

Pandas Combining Rows With Different Row Names

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

Solution 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

Post a Comment for "Pandas Combining Rows With Different Row Names"