Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: select rows if a specific column satisfies a certain condition

Say I have this dataframe df:

    A   B   C
0   1   1   2
1   2   2   2
2   1   3   1
3   4   5   2

Say you want to select all rows which column C is >1. If I do this:

newdf=df['C']>1

I only obtain True or False in the resulting df. Instead, in the example given I want this result:

    A   B   C
0   1   1   2
1   2   2   2
3   4   5   2

What would you do? Do you suggest using iloc?

like image 635
FaCoffee Avatar asked Sep 12 '25 06:09

FaCoffee


2 Answers

Use boolean indexing:

newdf=df[df['C']>1]
like image 86
jezrael Avatar answered Sep 14 '25 20:09

jezrael


use query

df.query('C > 1')

enter image description here

like image 35
piRSquared Avatar answered Sep 14 '25 20:09

piRSquared