Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas using row labels in boolean indexing

Tags:

python

pandas

So I have a DataFrame like this:

df = pd.DataFrame(np.random.randn(6, 3), columns=['a', 'b', 'c'])

      a         b         c
0  1.877317  0.109646  1.634978
1 -0.048044 -0.837403 -2.198505
2 -0.708137  2.342530  1.053073
3 -0.547951 -1.790304 -2.159123
4  0.214583 -0.856150 -0.477844
5  0.159601 -1.705155  0.963673

We can boolean index it like this

df[df.a > 0]

     a         b         c
0  1.877317  0.109646  1.634978
4  0.214583 -0.856150 -0.477844
5  0.159601 -1.705155  0.963673

We can also slice it via row labels like this:

df.ix[[0,2,4]]

    a         b         c
0  1.877317  0.109646  1.634978
2 -0.708137  2.342530  1.053073
4  0.214583 -0.856150 -0.477844

I would like to do both these operations at the same time (So I avoid making an unnecessary copy just to do the row label filter). How would I go about doing it?

Pseudo code for what I am looking for:

df[(df.a > 0) & (df.__index__.isin([0,2,4]))] 
like image 922
jason Avatar asked Feb 06 '13 08:02

jason


People also ask

Is boolean indexing possible in DataFrame?

Boolean indexing is defined as a very important feature of numpy, which is frequently used in pandas. Its main task is to use the actual values of the data in the DataFrame. We can filter the data in the boolean indexing in different ways, which are as follows: Access the DataFrame with a boolean index.

How do I name a row index in pandas?

You can use the rename() method of pandas. DataFrame to change column/index name individually. Specify the original name and the new name in dict like {original name: new name} to columns / index parameter of rename() . columns is for the column name, and index is for the index name.

What is the difference between LOC [] and ILOC []?

The main difference between pandas loc[] vs iloc[] is loc gets DataFrame rows & columns by labels/names and iloc[] gets by integer Index/position. For loc[], if the label is not present it gives a key error. For iloc[], if the position is not present it gives an index error.

How do you use boolean index in python?

In its simplest form, boolean indexing behaves as follows: Suppose x is an -dimensional array, and ind is a boolean-value array of the same shape as x . Then x[ind] returns a 1-dimensional array, which is formed by traversing x and ind using row-major ordering.


1 Answers

You nearly had it:

In [11]: df[(df.a > 0) & (df.index.isin([0, 2, 4]))]
Out[11]: 
          a         b         c
0  1.877317  0.109646  1.634978
4  0.214583 -0.856150 -0.477844
like image 150
Andy Hayden Avatar answered Oct 20 '22 04:10

Andy Hayden