17

How can I check if all values under col1 satisfy a condition such as > 2?

import pandas as pd

d = [
    {'col1': 3, 'col2': 'wasteful'},
    {'col1': 0, 'col2': 'hardly'},
    ]

df = pd.DataFrame(d)

I could go

if all(col1 > 2 for i, col1, col2 in df.itertuples()):
    #do stuff

but is there a more readable, faster and/or has less memory footprint way?

Nae
  • 14,209
  • 7
  • 52
  • 79
  • Possible duplicate of [Conditional Logic on Pandas DataFrame](https://stackoverflow.com/questions/14714181/conditional-logic-on-pandas-dataframe) – jjj Dec 19 '17 at 08:02
  • Can you be little more specific? There might be alot of better ways. – Sohaib Farooqi Dec 19 '17 at 08:02
  • @GarbageCollector mostly readability wise but performance wouldn't hurt either. – Nae Dec 19 '17 at 08:05

3 Answers3

21

I think you need create boolean mask and then all for check if all Trues:

print (df['col1'] > 2)
0     True
1    False
Name: col1, dtype: bool

print ((df['col1'] > 2).all())
False
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
2

You can also use numpy.where to check if all column of a dataframe satisfies a condition

import numpy as np
import pandas as pd

d = [
  {'col1': 3, 'col2': 'wasteful'},
  {'col1': 0, 'col2': 'hardly'},
]

df = pd.DataFrame(d)
print(all(np.where(df['col1'] > 2, True, False)))
#False
Sohaib Farooqi
  • 5,457
  • 4
  • 31
  • 43
0

A further option is the application of lambda-Functions

import pandas as pd
df = pd.DataFrame(
    [{'col1': 3, 'col2': 'wasteful'},
    {'col1': 0, 'col2': 'hardly'},
    {'col1': 9, 'col2': 'stuff'}])

print(df['col1'].apply(lambda x: True if ((x>2) & (x<8)) else False))

#result:
#0 True
#1 False
#2 False
Aroc
  • 1,022
  • 1
  • 10
  • 18