Search code examples
pythonpandasanalysis

How to change the sequence of some variables in a Pandas dataframe?


I have a dataframe that has 100 variables va1--var100. I want to bring var40, var20, and var30 to the front with other variables remain the original order. I've searched online, methods like

1: df[[var40, var20, var30, var1....]]
2: columns= [var40, var20, var30, var1...]

all require to specify all the variables in the dataframe. With 100 variables exists in my dataframe, how can I do it efficiently?

I am a SAS user, in SAS, we can use a retain statement before the set statement to achieve the goal. Is there a equivalent way in python too?

Thanks


Solution

  • Consider reindex with a conditional list comprehension:

    first_cols = ['var30', 'var40', 'var20']
    
    df = df.reindex(first_cols + [col for col in df.columns if col not in first_cols], 
                    axis = 'columns')