1

I have a dataframe with many columns.

df=pd.DataFrame(data=np.random.rand(100,1000))

I need to have two specific column, say [793] and [642] as the first ones, while the order of the others does not matter.

I could do

df.columns=[793,642,...the rest...]

However, my columns are variable, as well as the reference ones.

1 Answer 1

3

Use difference for all columns without specified in list with prepend list L for new columns names, last change ordering by subset by []:

df=pd.DataFrame(data=np.random.rand(100,1000))

L = [793, 642]

cols = L + df.columns.difference(L).tolist()
#another solution
#cols = np.concatenate([L, df.columns.difference(L)])
#list comprehension solution
#cols = L + [x for x in df.columns if not x in L]

df = df[cols]
print (df.head(2))
        793       642       0         1         2         3         4    \
0  0.462103  0.811223  0.491396  0.701752  0.494450  0.352717  0.345460   
1  0.840597  0.852080  0.681095  0.014459  0.963252  0.972862  0.490964   

        5         6         7      ...          990       991       992  \
0  0.718141  0.199168  0.379924    ...     0.279972  0.963898  0.987907   
1  0.151226  0.625833  0.428249    ...     0.069179  0.045112  0.328453   

        993       994       995       996       997       998       999  
0  0.402805  0.243648  0.624790  0.864440  0.653621  0.066524  0.072025  
1  0.894080  0.451285  0.538485  0.834018  0.926311  0.032849  0.095636  

[2 rows x 1000 columns]
Sign up to request clarification or add additional context in comments.

Comments

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.