I have created a Pandera validation schema for a Pandas dataframe with ~150 columns, like the first two rows in the schema below. The single column validation is working, but how can I combine two or more columns for validation? I found two related questions here and here, but I still don't manage to build a valid schema. Row nr 3 (0.0/1.0) is not valid:
preg nr_preg
1.0 2.0
0.0 NaN
0.0 1.0
NaN NaN
import pandas as pd
import numpy as np
import pandera as pa
df = pd.DataFrame({'preg': [1, 0, 0, np.nan], 'nr_preg': [2, np.nan, 1, np.nan]})
schema = pa.DataFrameSchema({
'preg': pa.Column(float, pa.Check.isin([1, 0]), nullable=True),
'nr_preg': pa.Column(float, pa.Check.in_range(1, 10), nullable=True),
# ...
# not working:
# if preg=0 -> nr_preg must be NaN
'preg': pa.Column(float, pa.Check(lambda s: s['preg'] == 0 & s['nr_preg'].isnull() == False), nullable=True)
})