我认为具有比较值的新列的字典是个好主意。
d = {'bool1':'20190731', 'bool2':'20190803', 'bool3':'20190813'}
然后可以在循环中创建新列:
for k, v in d.items():
df[k] = (df['date'] > pd.to_datetime(v)).astype(int)
#alternative
#df[k] = np.where(df['date'] > pd.to_datetime(v), 1, 0)
为了提高性能,在 numpy 中使用广播:
rng = pd.date_range('20190731', periods=20)
df = pd.DataFrame({'date': rng})
d = {'bool1':'20190731', 'bool2':'20190803', 'bool3':'20190813'}
#pandas 0.24+
mask = df['date'].to_numpy()[:, None] > pd.to_datetime(list(d.values())).to_numpy()
#pandas below
#mask = df['date'].values[:, None] > pd.to_datetime(list(d.values())).values
arr = np.where(mask, 1, 0)
df = df.join(pd.DataFrame(arr, columns=d.keys()))
print (df)
date bool1 bool2 bool3
0 2019-07-31 0 0 0
1 2019-08-01 1 0 0
2 2019-08-02 1 0 0
3 2019-08-03 1 0 0
4 2019-08-04 1 1 0
5 2019-08-05 1 1 0
6 2019-08-06 1 1 0
7 2019-08-07 1 1 0
8 2019-08-08 1 1 0
9 2019-08-09 1 1 0
10 2019-08-10 1 1 0
11 2019-08-11 1 1 0
12 2019-08-12 1 1 0
13 2019-08-13 1 1 0
14 2019-08-14 1 1 1
15 2019-08-15 1 1 1
16 2019-08-16 1 1 1
17 2019-08-17 1 1 1
18 2019-08-18 1 1 1
19 2019-08-19 1 1 1