I have a pandas df like this
id v1 v2 v3 v4
1 a a b b
2 x f f a
How can I order it with a based on values from a set such as
setorder = ('1','3','2','4')
yields
id v1 v3 v2 v4
1 a b a b
2 x f f a
Thanks
I have a pandas df like this
id v1 v2 v3 v4
1 a a b b
2 x f f a
How can I order it with a based on values from a set such as
setorder = ('1','3','2','4')
yields
id v1 v3 v2 v4
1 a b a b
2 x f f a
Thanks
Set is not ordered, so is possible use tuple
like in sample data:
tupleorder = ('1','3','2','4')
df1 = df[['id'] + [f'v{x}' for x in tupleorder]]
print (df1)
id v1 v3 v2 v4
0 1 a b a b
1 2 x f f a
Try this:
cols = ('1','3','2','4')
cols = [f"v{x}" for x in cols]
df[cols]
Print out:
v1 v3 v2 v4
0 a b a b
1 x f f a
You alternatively can use a list for cols
which is ordered as well.