Table of contents
Create synthetic data
First, let's generate artificial data and use it to create a Pandas dataframe.
import pandas as pdimport numpy as npnp.random.seed(42)data = np.random.uniform(low=-9.0, high=100.0, size=(10,4))df = pd.DataFrame(data=data, columns=['A','B','C','D']).round(1)print(df)
The code above will generate:
A B C D0 31.8 94.6 70.8 56.31 8.0 8.0 -2.7 85.42 56.5 68.2 -6.8 96.73 81.7 14.1 10.8 11.04 24.2 48.2 38.1 22.75 57.7 6.2 22.8 30.96 40.7 76.6 12.8 47.17 55.6 -3.9 57.2 9.68 -1.9 94.4 96.3 79.19 24.2 1.6 65.6 39.0
Using any
Checking for negative values in a Pandas dataframe can be done using the any() method along the axis 1:
(df < 0).any(axis=1)
returns
0 False1 True2 True3 False4 False5 False6 False7 True8 True9 Falsedtype: bool
Using min()
Another way to achieve this task is by making use of the min() method.
df.min(axis=1)
returns
0 31.81 -2.72 -6.83 10.84 22.75 6.26 12.87 -3.98 -1.99 1.6dtype: float64
