Skip to content Skip to sidebar Skip to footer

Pandas How To Create A Boolean Column Based On Other Boolean Columns In The Df

I have the following df, inv_date inv_id 2017-10-01 100117 2018-04-02 040218 2018-05-06 060518 where inv_date is of datetime dtype, and inv_id is str; I want

Solution 1:

You can use np.logical_or.reduce:

a = df1['inv_id_1'].between(df1['inv_date'] - pd.Timedelta(180, unit='d'), df1['inv_date'] + pd.Timedelta(180, unit='d'))
b = df1['inv_id_2'].between(df1['inv_date'] - pd.Timedelta(180, unit='d'), df1['inv_date'] + pd.Timedelta(180, unit='d'))

c = [a,b]
df['dummy_inv_id'] = np.logical_or.reduce(c)
print (df)
    inv_date  inv_id  dummy_inv_id
0 2017-10-01  100117          True
1 2018-04-02   40218          True
2 2018-05-06   60518          True

Post a Comment for "Pandas How To Create A Boolean Column Based On Other Boolean Columns In The Df"