我正在尝试根据以下两个列时间戳的条件创建一个新列,
def time_delta(df):
if df['a_time'] > df['b_time']:
res = 'Early'
else:
res = 'Late'
return res
或
df['new_col'] = np.where(df['a_time'] > df['b_time'], 'Early', 'Late')
但是a_time列有时具有NaN值,如果a_time为NaN,我希望新列中的结果或值也为NaN,我该怎么做或调整我的值呢?
所需的输出
time_a | time_b | new_column
2019-08-19 22:25:26.133 2019-08-19 23:00:00.000 Before_b
NaN 2019-08-19 22:00:00.000 NaN
2019-08-19 23:00:00.000 2019-08-19 20:00:00.000 After_b
谢谢!
答案 0 :(得分:2)
您可以使用numpy.isnat
对其进行修复。
df['new_col'] = np.where(df['a_time'] > df['b_time'], 'Early', 'Late')
df.loc[(np.isnat(df['a_time']) | np.isnat(df['b_time'])), 'new_col'] = np.NaN
答案 1 :(得分:1)
您可以使用np.select
df['new_column'] = np.select([df.isna().any(1), df.time_a > df.time_b], [pd.NaT, 'Early'], 'Late')
Out[923]:
time_a time_b new_column
0 2019-08-19 22:25:26.133 2019-08-19 23:00:00 Late
1 NaT 2019-08-19 22:00:00 NaT
2 2019-08-19 23:00:00.000 2019-08-19 20:00:00 Early