Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]]
like image 434
lignin Avatar asked May 02 '18 20:05

lignin


People also ask

How do I slice columns in pandas?

To slice the columns, the syntax is df. loc[:,start:stop:step] ; where start is the name of the first column to take, stop is the name of the last column to take, and step as the number of indices to advance after each extraction; for example, you can select alternate columns.

How do I select multiple columns using LOC?

Using df[] & loc[] to Select Multiple Columns by NameBy using df[] & pandas. DataFrame. loc[] you can select multiple columns by names or labels. To select the columns by names, the syntax is df.

How do I select selective columns in pandas?

You can use the filter function of the pandas dataframe to select columns containing a specified string in column names. The parameter like of the . filter function defines this specific string. If a column name contains the string specified, that column will be selected and dataframe will be returned.


1 Answers

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])
like image 160
cs95 Avatar answered Sep 19 '22 05:09

cs95