熊猫如何基于df中的其他布尔列创建布尔列

时间:2018-09-05 10:34:21

标签: python python-3.x pandas dataframe

我有以下df

inv_date        inv_id
2017-10-01      100117
2018-04-02      040218
2018-05-06      060518

其中inv_datedatetime dtype,而inv_idstr;我想根据以下inv_iddatetime转换为formats

formats = {'%m%d%y': 6, '%d%m%y': 6}
L = [pd.to_datetime(s.str[:v], format=k, errors='coerce') for k, v in formats.items()]
df1 = pd.concat(L, axis=1, keys=[s.name + '_' + str(i) for i, s in zip(count(), L)])
df1 = df.apply(lambda x: x.where(x.between('2000-01-01', datetime.now())))

我想创建一个布尔列dummy_inv_id,如果任何非NaT转换的Truedatetime的+/- 180天内,则将其设置为inv_date

df1 = df1.assign(inv_date=df['inv_date'])
df1['inv_id_1'].between(df1['inv_date'] - Timedelta(180, unit='d'), df1['inv_date'] + Timedelta(180, unit='d'))
df1['inv_id_2'].between(df1['inv_date'] - Timedelta(180, unit='d'), df1['inv_date'] + Timedelta(180, unit='d'))

我想知道如何集中考虑inv_id_1中的所有日期时间列(inv_id_2df1),因此,如果有人在inv_date +/- 180 days之间,则分配truedf对应的日期时间;

结果df如下,

inv_date        inv_id    dummy_inv_id
2017-10-01      100117    true
2018-04-02      040218    true
2018-05-06      060518    true

1 个答案:

答案 0 :(得分:1)

您可以使用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