Issue
Is there a way to reorder columns in pandas dataframe based on my personal preference (i.e. not alphabetically or numerically sorted, but more like following certain conventions)?
Simple example:
frame = pd.DataFrame({
'one thing':[1,2,3,4],
'second thing':[0.1,0.2,1,2],
'other thing':['a','e','i','o']})
produces this:
one thing other thing second thing
0 1 a 0.1
1 2 e 0.2
2 3 i 1.0
3 4 o 2.0
But instead, I would like this:
one thing second thing other thing
0 1 0.1 a
1 2 0.2 e
2 3 1.0 i
3 4 2.0 o
(Please, provide a generic solution rather than specific to this case. Many thanks.)
Solution
Just select the order yourself by typing in the column names. Note the double brackets:
frame = frame[['column I want first', 'column I want second'...etc.]]
Answered By - A.Kot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.