Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selecting rows based on multiple column values in pandas dataframe

Tags:

python

pandas

I have a pandas DataFrame df:

import pandas as pd  data = {"Name": ["AAAA", "BBBB"],         "C1": [25, 12],         "C2": [2, 1],         "C3": [1, 10]}  df = pd.DataFrame(data) df.set_index("Name") 

which looks like this when printed (for reference):

      C1  C2  C3 Name             AAAA  25   2   1 BBBB  12   1  10 

I would like to choose rows for which C1, C2 and C3 have values between 0 and 20.

Can you suggest an elegant way to select those rows?

like image 509
Ssank Avatar asked Mar 23 '15 19:03

Ssank


People also ask

How do you select rows of pandas DataFrame based on a multiple value of a column?

You can select the Rows from Pandas DataFrame based on column values or based on multiple conditions either using DataFrame. loc[] attribute, DataFrame. query() or DataFrame. apply() method to use lambda function.

How can pandas select rows based on multiple conditions?

You can get pandas. Series of bool which is an AND of two conditions using & . Note that == and ~ are used here as the second condition for the sake of explanation, but you can use !=

How do you filter a DataFrame based on multiple column values?

To filter pandas DataFrame by multiple columns. When we filter a DataFrame by one column, we simply compare that column values against a specific condition but when it comes to filtering of DataFrame by multiple columns, we need to use the AND (&&) Operator to match multiple columns with multiple conditions.


2 Answers

I think below should do it, but its elegance is up for debate.

new_df = old_df[((old_df['C1'] > 0) & (old_df['C1'] < 20)) & ((old_df['C2'] > 0) & (old_df['C2'] < 20)) & ((old_df['C3'] > 0) & (old_df['C3'] < 20))] 
like image 144
kennes Avatar answered Oct 01 '22 19:10

kennes


Shorter version:

In [65]:  df[(df>=0)&(df<=20)].dropna() Out[65]:    Name  C1  C2  C3 1  BBBB  12   1  10 
like image 39
EdChum Avatar answered Oct 01 '22 20:10

EdChum