1

I want to order a DataFrame by multiple regex. That is to say, for example in this DataFrame

df = pd.DataFrame({'Col1': [20, 30],
                    'Col2': [50, 60],
                    'Pol2': [50, 60]})

get the columns beginning with P before the ones beginning with C.

I've discovered that you can filter with one regex like

df.filter(regex = "P*")

but I can't do that with more levels.

UPDATE: I want to do that in one instruction, I'm already able to use a list of regex and concatenate the columns in another DataFrame.

2 Answers 2

4

I believe you need list of DataFrames filtered by regexes in list with concat:

reg = ['^P','^C']
df1 = pd.concat([df.filter(regex = r) for r in reg], axis=1)
print (df1)
   Pol2  Col1  Col2
0    50    20    50
1    60    30    60
Sign up to request clarification or add additional context in comments.

2 Comments

Look at the update I made few seconds ago. Thank you anyway
@Angelo - Not sure if understand but I can't do that with more levels. Can you explain more?
0

you can just re-order columns by regular assignment.

export the colums to a sorted list, and index by it.

try:

import pandas as pd

df = pd.DataFrame({'Col1': [20, 30],
                   'Pol2': [50, 60],
                    'Col2': [50, 60],
                    })

df = df[sorted(df.columns.to_list(), key=lambda col: col.startswith("P"), reverse=True)]

print(df)

1 Comment

note: if you need a more complex regex, you can modify the key to your needs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.