Reputation: 15315
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?
Upvotes: 19
Views: 26085
Reputation: 1263
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
Upvotes: 1
Reputation: 5666
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
Upvotes: 2