3

I have a dataFrame and I want a range of specific rows and at the same time a range that is composed of a continuous range of columns plus an additional one. Included is a piece of code that produces such a dataFrame

import pandas as pd 
import numpy as np
np.random.seed(5)
dF = pd.DataFrame(np.random.randint(100, size=(100, 6)), 
              columns=list('ABCDEF'), 
              index=['R{}'.format(i) for i in range(100)])
dF.head()

This works fine:

dF.loc[:, 'C':'E']

But I need something like this, that produces an error:

dF.loc['R95':, ['A':'C', 'F']]

The expected result has to include from row 'R95' onwards and columns 'A', 'C' and 'F'

3
  • 2
    dF.loc['R95':, ['C','D', 'F']] considering "The expected result has to include from row 'R95' onwards and columns 'C', 'D' and 'F'" Commented May 31, 2019 at 17:16
  • @anky_91 I would like to use the ":" within the solution for the selection. Commented May 31, 2019 at 17:21
  • I don't think you can combine slice and commas in the indexing. It's not supported. Read here Commented May 31, 2019 at 17:27

3 Answers 3

3

If you can just use the index you can do:

dF.iloc[95:, np.r_[0:3, -1]]

    A   B   C   F
R95 19  7   76  0
R96 57  31  86  64
R97 51  12  59  33
R98 24  7   68  69
R99 81  20  86  70

which is a lot cleaner.

Sign up to request clarification or add additional context in comments.

Comments

2

pandas indexing allows the following ways to indexing a dataframe (quoting from the docs):

  • A single label, e.g. 5 or 'a' (Note that 5 is interpreted as a label of the index. This use is not an integer position along the index.).
  • A list or array of labels ['a', 'b', 'c'].
  • A slice object with labels 'a':'f' (Note that contrary to usual python slices, both the start and the stop are included, when present in the index! See Slicing with labels.).
  • A boolean array
  • A callable function with one argument (the calling Series, DataFrame or Panel) and that returns valid output for indexing (one of the above).

So you need to use something a bit more complex.

For example, you could use pandas.concat to select the columns separately and then join together the dataframes:

pd.concat([dF.loc['R95':, 'A':'C'], dF.loc['R95':,'F']], axis=1)

This gives:

      A   B   C   F
R95  19   7  76   0
R96  57  31  86  64
R97  51  12  59  33
R98  24   7  68  69
R99  81  20  86  70

Comments

0

This is an ugly solution but it still has a colon in it

df.loc['R95':, df.loc[:,'A':'C'].columns.tolist()+['F']]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.