lignin
lignin

Reputation: 459

Pandas Dataframe select multiple discontinuous columns/slices

I have dataframe with >100 columns. I am trying to select the columns 0~32 and #83. It seems that 1 slice works fine with the code below.

df_new = df[df.columns[0:32]]

It does not work with 2 slices code below though. How do I fix the problem?

df_new = df[df.columns[0:32, 83]]

Upvotes: 5

Views: 2744

Answers (3)

lishu luo
lishu luo

Reputation: 116

I am also interest in this problem. We can choose several separate lines or columns. But it seems slicing operation could be done only once on each axis. Like the following one.

new_df=df.iloc[[2,3,4],[3:4]]

Maybe we can slice first and then concatenate them together.

df1=df.iloc[[2:4],:]
df2=df.iloc[[8:10],:]
new_df=pd.concat([df1,df2],axis=0)

Upvotes: 0

Anton vBR
Anton vBR

Reputation: 18916

np.r_ is an excellent answer. The other approach would be to construct it with list and range.

Consider this example:

import pandas as pd

df = pd.DataFrame([range(10)],range(10))
cols = list(range(0,5))+[8]              # 0,1,2,3,4,8
df.iloc[:,cols]

Upvotes: 3

cs95
cs95

Reputation: 402603

Use the np.r_ indexer in conjunction with iloc, like this:

df.iloc[:, np.r_[0:32, 83]]

np.r_[0:32, 83]

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 83])

Upvotes: 10

Related Questions