pandas 如何基于df中的其他布尔列创建布尔列 [英] pandas how to create a boolean column based on other boolean columns in the df
问题描述
我有以下df
,
inv_date inv_id
2017-10-01 100117
2018-04-02 040218
2018-05-06 060518
其中inv_date
是datetime
dtype
,而inv_id
是str
;我想根据以下formats
,
where inv_date
is of datetime
dtype
, and inv_id
is str
; I want to convert inv_id
into datetime
, based on the following 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转换的datetime
在inv_date
的+/- 180天内,则将其设置为True
,
I want to create a boolean column dummy_inv_id
, which is set to True
if any of non-NaT converted datetime
is within +/- 180 days of 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'))
我想知道如何共同考虑df1
中的所有日期时间列(inv_id_1
和inv_id_2
),因此,如果任何人在inv_date +/- 180 days
之间,则将true
分配给df
以获得对应的日期时间;
I am wondering how to consider all datetime columns (inv_id_1
and inv_id_2
) in df1
collectively so if anyone is between inv_date +/- 180 days
, then assign true
to df
for corresponding datetime;
所以结果df
看起来像是
inv_date inv_id dummy_inv_id
2017-10-01 100117 true
2018-04-02 040218 true
2018-05-06 060518 true
推荐答案
您可以使用 np.logical_or.reduce
:
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
这篇关于 pandas 如何基于df中的其他布尔列创建布尔列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!