Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering on index levels in a pandas.DataFrame

Tags:

python

pandas

If I have a multiindex dataframe:

import pandas as pd
df = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]],columns=['a','b','c']).set_index(['a','b'])

I can simply filter the dataframe on a column, for example:

df[df.c>4]

But to do the same on the level of an index, say "b", I can't do:

df[df.b>4]

Instead I can do:

df[df.index.get_level_values('b')>4]

But is there a less verbose way to do this?

like image 374
dlm Avatar asked Jul 13 '17 18:07

dlm


People also ask

How to filter pandas Dataframe based on the index?

Here is the syntax that you can use to filter Pandas DataFrame based on the index: df = df.filter(like = 'index to keep', axis=0) Let’s review an example to see how to apply the above syntax in practice. The Example. Suppose that you created the DataFrame below:

How do I select a Dataframe by column value in pandas?

Filter Pandas Dataframe by Column Value Pandas makes it incredibly easy to select data by a column value. This can be accomplished using the index chain method. Select Dataframe Values Greater Than Or Less Than

How to filter rows in pandas to get answers faster?

Filter rows in Pandas to get answers faster. Not all data is created equal. Filtering rows in pandas removes extraneous or incorrect data so you are left with the cleanest data set available. You can filter by values, conditions, slices, queries, and string methods.

How to filter rows by column value in a Dataframe?

How to Filter Rows by Column Value Often, you want to find instances of a specific value in your DataFrame. You can easily filter rows based on whether they contain a value or not using the .loc indexing method. For this example, you have a simple DataFrame of random integers arrayed across two columns and 10 rows:


1 Answers

You can use query for better readability

In [795]: df.query('b > 4')
Out[795]:
     c
a b
4 5  6
7 8  9
like image 92
Zero Avatar answered Oct 03 '22 22:10

Zero