Reputation: 3046
Rearranging columns in pandas has been asked before in many threads.
I can print
my columns and copy paste whatever I need to reorder in pandas
. However, I am wondering if I have more than 20 columns, can I rearrange using columns numbers? I know in R
this can be done something like this,
new_df <- my_df[,c(1:9, 11:21, 10)]
I attempted the same below in pandas
, but getting a SyntaxError
,
new_df = my_df[[:, 1:9, 11:21, 10]]
I have been searching and could not find a document to refer to get an answer. Is there anything like that in pandas
that I can do in one line like in R
?
Upvotes: 2
Views: 1660
Reputation: 863031
I think you need numpy.r_
for concanecate indices:
new_df = my_df.iloc[:, np.r_[1:9, 11:21, 10]]
Sample:
np.random.seed(100)
my_df = pd.DataFrame(np.random.randint(10, size=(3,30)))
new_df = my_df.iloc[:, np.r_[1:9, 11:21, 10]]
print (new_df)
1 2 3 4 5 6 7 8 11 12 13 14 15 16 17 18 19 20 10
0 8 3 7 7 0 4 2 5 2 1 0 8 4 0 9 6 2 4 2
1 7 0 2 9 9 3 2 5 0 7 6 2 0 8 2 5 1 8 1
2 6 3 4 7 6 3 9 0 5 7 6 6 2 4 2 7 1 6 4
new_df = my_df.iloc[:, np.r_[1:10, 11:22, 10]]
print (new_df)
1 2 3 4 5 6 7 8 9 11 ... 13 14 15 16 17 18 19 20 \
0 8 3 7 7 0 4 2 5 2 2 ... 0 8 4 0 9 6 2 4
1 7 0 2 9 9 3 2 5 8 0 ... 6 2 0 8 2 5 1 8
2 6 3 4 7 6 3 9 0 4 5 ... 6 6 2 4 2 7 1 6
21 10
0 1 2
1 1 1
2 6 4
[3 rows x 21 columns]
Solution with range
:
a = list(range(1,10)) + list(range(11,22)) + [10]
new_df = my_df.iloc[:, a]
print (new_df)
1 2 3 4 5 6 7 8 9 11 ... 13 14 15 16 17 18 19 20 \
0 8 3 7 7 0 4 2 5 2 2 ... 0 8 4 0 9 6 2 4
1 7 0 2 9 9 3 2 5 8 0 ... 6 2 0 8 2 5 1 8
2 6 3 4 7 6 3 9 0 4 5 ... 6 6 2 4 2 7 1 6
21 10
0 1 2
1 1 1
2 6 4
[3 rows x 21 columns]
Upvotes: 3
Reputation: 210882
we can use np.r_[]:
new_df = my_df.iloc[:, np.r_[1:9, 11:21, 10]]
Demo:
In [7]: df = pd.DataFrame(np.random.randint(5, size=(2, 21)))
In [8]: df
Out[8]:
0 1 2 3 4 5 6 7 8 9 ... 11 12 13 14 15 16 17 18 19 20
0 2 1 2 2 1 0 4 4 4 2 ... 0 4 4 4 3 2 1 2 1 4
1 1 4 4 4 1 3 4 4 3 3 ... 1 2 3 4 2 0 1 0 2 1
[2 rows x 21 columns]
In [9]: df.iloc[:, np.r_[1:9, 11:21, 10]]
Out[9]:
1 2 3 4 5 6 7 8 11 12 13 14 15 16 17 18 19 20 10
0 1 2 2 1 0 4 4 4 0 4 4 4 3 2 1 2 1 4 0
1 4 4 4 1 3 4 4 3 1 2 3 4 2 0 1 0 2 1 0
Upvotes: 4