如何检查条件下的所有值是否都col1满足例如> 2?
import pandas as pd
d = [
{'col1': 3, 'col2': 'wasteful'},
{'col1': 0, 'col2': 'hardly'},
]
df = pd.DataFrame(d)
Run Code Online (Sandbox Code Playgroud)
我可以去
if all(col1 > 2 for i, col1, col2 in df.itertuples()):
#do stuff
Run Code Online (Sandbox Code Playgroud)
但是是否有更易读,更快速和/或更少的内存占用方式?
我认为您需要创建布尔掩码,然后all检查是否所有Trues:
print (df['col1'] > 2)
0 True
1 False
Name: col1, dtype: bool
print ((df['col1'] > 2).all())
False
Run Code Online (Sandbox Code Playgroud)