Thanasis
Thanasis

Reputation: 725

Sort columns in dataframe based on float suffix in headers

I have the following dataframe:

data = [{'Po_+0.00': 1, 'Po_+0.25': 1.5, 'Po_+0.50': 2,'Po_-0.25': 1.3, 'Po_-0.50': 1.6}]
df = pd.DataFrame(data, index =[1])

enter image description here My goal is to to sort the columns like that:

enter image description here

I tried various ways but never managed to get the correct result.

Any idea?

Thanks in advance.

Upvotes: 1

Views: 544

Answers (2)

jezrael
jezrael

Reputation: 862841

Use sorted with parameter key and lambda function for convert next 3 values to floats:

df = df[sorted(df.columns, key=lambda x: float(x[3:]))]
print (df)
   Po_-0.50  Po_-0.25  Po_+0.00  Po_+0.25  Po_+0.50
1       1.6       1.3         1       1.5         2

Upvotes: 1

Space Impact
Space Impact

Reputation: 13255

Use argsort taking numerical part of column names:

df = df[df.columns[df.columns.str[3:].astype(float).argsort()]]
#Alternative
#df = df.iloc[:, df.columns.str[3:].astype(float).argsort()]

print(df)
   Po_-0.50  Po_-0.25  Po_+0.00  Po_+0.25  Po_+0.50
1       1.6       1.3         1       1.5         2

Upvotes: 1

Related Questions