Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

python - Slicing multiple column ranges from a dataframe using iloc

I have a df with 32 columns

df.shape
(568285, 32)

I am trying to rearrange the columns in a specific way, and drop the first column using iloc

 df = df.iloc[:,[31,[1:23],24,25,26,28,27,29,30]]
                         ^
SyntaxError: invalid syntax

is this the right way to do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use the np.r_ indexer.

class RClass(AxisConcatenator)
 |  Translates slice objects to concatenation along the first axis.
 |  
 |  This is a simple way to build up arrays quickly. There are two use cases.

df = df.iloc[:, np.r_[31, 1:23, 24, 25, 26, 28, 27, 29, 30]]

df

     0     1     2     3     4     5     6     7     8     9   ...     40  
A  33.0  44.0  68.0  31.0   NaN  87.0  66.0   NaN  72.0  33.0  ...   71.0   
B   NaN   NaN  77.0  98.0   NaN  48.0  91.0  43.0   NaN  89.0  ...   38.0   
C  45.0  55.0   NaN  72.0  61.0  87.0   NaN  99.0  96.0  75.0  ...   83.0   
D   NaN   NaN   NaN  58.0   NaN  97.0  64.0  49.0  52.0  45.0  ...   63.0   

     41    42    43    44    45    46    47    48    49  
A   NaN  87.0  31.0  50.0  48.0  73.0   NaN   NaN  81.0  
B  79.0  47.0  51.0  99.0  59.0   NaN  72.0  48.0   NaN  
C  93.0   NaN  95.0  97.0  52.0  99.0  71.0  53.0  69.0  
D   NaN  41.0   NaN   NaN  55.0  90.0   NaN   NaN  92.0

out = df.iloc[:, np.r_[31, 1:23, 24, 25, 26, 28, 27, 29, 30]]
out 
     31    1     2     3     4     5     6     7     8     9   ...     20  
A  99.0  44.0  68.0  31.0   NaN  87.0  66.0   NaN  72.0  33.0  ...   66.0   
B  42.0   NaN  77.0  98.0   NaN  48.0  91.0  43.0   NaN  89.0  ...    NaN   
C  77.0  55.0   NaN  72.0  61.0  87.0   NaN  99.0  96.0  75.0  ...   76.0   
D  95.0   NaN   NaN  58.0   NaN  97.0  64.0  49.0  52.0  45.0  ...   71.0   

     21    22    24    25    26    28    27    29    30  
A   NaN  40.0  66.0  87.0  97.0  68.0   NaN  68.0   NaN  
B  95.0   NaN  47.0  79.0  47.0   NaN  83.0  81.0  57.0  
C   NaN  75.0  46.0  84.0   NaN  50.0  41.0  38.0  52.0  
D   NaN  74.0  41.0  55.0  60.0   NaN   NaN  84.0   NaN  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...