简体   繁体   中英

Rearrange columns using column numbers- Pythonic way

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 ?

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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM