'Set order of columns in pandas dataframe
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 1:[1]
You can use this:
columnsTitles = ['onething', 'secondthing', 'otherthing']
frame = frame.reindex(columns=columnsTitles)
Solution 2:[2]
Here is a solution I use very often. When you have a large data set with tons of columns, you definitely do not want to manually rearrange all the columns.
What you can and, most likely, want to do is to just order the first a few columns that you frequently use, and let all other columns just be themselves. This is a common approach in R. df %>%select(one, two, three, everything())
So you can first manually type the columns that you want to order and to be positioned before all the other columns in a list cols_to_order.
Then you construct a list for new columns by combining the rest of the columns:
new_columns = cols_to_order + (frame.columns.drop(cols_to_order).tolist())
After this, you can use the new_columns as other solutions suggested.
import pandas as pd
frame = pd.DataFrame({
'one thing': [1, 2, 3, 4],
'other thing': ['a', 'e', 'i', 'o'],
'more things': ['a', 'e', 'i', 'o'],
'second thing': [0.1, 0.2, 1, 2],
})
cols_to_order = ['one thing', 'second thing']
new_columns = cols_to_order + (frame.columns.drop(cols_to_order).tolist())
frame = frame[new_columns]
one thing second thing other thing more things
0 1 0.1 a a
1 2 0.2 e e
2 3 1.0 i i
3 4 2.0 o o
Solution 3:[3]
You could also do something like df = df[['x', 'y', 'a', 'b']]
import pandas as pd
frame = pd.DataFrame({'one thing':[1,2,3,4],'second thing':[0.1,0.2,1,2],'other thing':['a','e','i','o']})
frame = frame[['second thing', 'other thing', 'one thing']]
print frame
second thing other thing one thing
0 0.1 a 1
1 0.2 e 2
2 1.0 i 3
3 2.0 o 4
Also, you can get the list of columns with:
cols = list(df.columns.values)
The output will produce something like this:
['x', 'y', 'a', 'b']
Which is then easy to rearrange manually.
Solution 4:[4]
Construct it with a list instead of a dictionary
frame = pd.DataFrame([
[1, .1, 'a'],
[2, .2, 'e'],
[3, 1, 'i'],
[4, 4, 'o']
], columns=['one thing', 'second thing', 'other thing'])
frame
one thing second thing other thing
0 1 0.1 a
1 2 0.2 e
2 3 1.0 i
3 4 4.0 o
Solution 5:[5]
You can also use OrderedDict:
In [183]: from collections import OrderedDict
In [184]: data = OrderedDict()
In [185]: data['one thing'] = [1,2,3,4]
In [186]: data['second thing'] = [0.1,0.2,1,2]
In [187]: data['other thing'] = ['a','e','i','o']
In [188]: frame = pd.DataFrame(data)
In [189]: frame
Out[189]:
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
Solution 6:[6]
Add the 'columns' parameter:
frame = pd.DataFrame({
'one thing':[1,2,3,4],
'second thing':[0.1,0.2,1,2],
'other thing':['a','e','i','o']},
columns=['one thing', 'second thing', 'other thing']
)
Solution 7:[7]
Try indexing (so you want a generic solution not only for this, so index order can be just what you want):
l=[0,2,1] # index order
frame=frame[[frame.columns[i] for i in l]]
Now:
print(frame)
Is:
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
Solution 8:[8]
Even though it's an old question, you can also use loc and iloc:
frame = frame.loc[:, ['column I want first', 'column I want second', "other thing"]]
frame = frame.iloc[:, [1, 3, 2]]
Solution 9:[9]
I find this to be the most straightforward and working:
df = pd.DataFrame({
'one thing':[1,2,3,4],
'second thing':[0.1,0.2,1,2],
'other thing':['a','e','i','o']})
df = df[['one thing','second thing', 'other thing']]
Solution 10:[10]
df = df.reindex(columns=["A", "B", "C"])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
