Tom
Tom

Reputation: 251

DataFrame Subset

I have a dataframe already and am subsetting some of it to another dataframe.

I do that like this:

D = njm[['svntygene', 'intgr', 'lowgr', 'higr', 'lumA', 'lumB', 'wndres', 'nlbrst',        'Erneg', 'basallike']]

I want to try and set it by the integer position though, something like this:

D = njm.iloc[1:, 2:, 3:, 7:]

But I get an error. How would I do this part? Read the docs but could not find a clear answer.

Also, is it possible to pass a list to this as values too?

Thanks.

Upvotes: 0

Views: 705

Answers (1)

DSM
DSM

Reputation: 353009

This is covered in the iloc section of the documentation: you can pass a list with the desired indices.

>>> df = pd.DataFrame(np.random.random((5,5)),columns=list("ABCDE"))
>>> df
          A         B         C         D         E
0  0.605594  0.229728  0.390391  0.754185  0.516801
1  0.384228  0.106261  0.457507  0.833473  0.786098
2  0.364943  0.664588  0.330835  0.846941  0.229110
3  0.025799  0.681206  0.235821  0.418825  0.878566
4  0.811800  0.761962  0.883281  0.932983  0.665609
>>> df.iloc[:,[1,2,4]]
          B         C         E
0  0.229728  0.390391  0.516801
1  0.106261  0.457507  0.786098
2  0.664588  0.330835  0.229110
3  0.681206  0.235821  0.878566
4  0.761962  0.883281  0.665609

Upvotes: 1

Related Questions