I'm trying to transform a DataFrame with a dynamic number of a_P columns looking like this
a1_P a2_P weight
0 33297.81 17407.93 14733.23
1 58895.18 43013.57 86954.04
into a new DataFrame, looking like this (sorted by P)
P weight
0 17407.93 14733.23
1 33297.81 14733.23
2 43013.57 86954.04
3 58895.18 86954.04
So what I'm trying so far is
names = ["a1", "a2"]
p = pd.DataFrame(columns=["P", "weight"])
for i in range(0, len(names)):
p += df[["{}_P".format(names[i]), "weight"]]
and to sort it afterwards but this does not work because columnnames are not identical I guess.