我有一个如下所示的数据框:
A B datetime
10 NaN 12-03-2020 04:43:11
NaN 20 13-03-2020 04:43:11
NaN NaN 14-03-2020 04:43:11
NaN NaN 15-03-2020 04:43:11
NaN NaN 16-03-2020 04:43:11
NaN 50 17-03-2020 04:43:11
20 NaN 18-03-2020 04:43:11
NaN 30 19-03-2020 04:43:11
NaN NaN 20-03-2020 04:43:11
30 30 21-03-2020 04:43:11
40 NaN 22-03-2020 04:43:11
NaN 10 23-03-2020 04:43:11
这里的逻辑是,如果列A是notna(),列B的下一个最近的非NaN值是notna(),则返回列B的时间戳
对于此逻辑,我使用以下代码:
df['cond1'] = df['A'].notna()
for t in range(1,5):
if df['cond1'] == True:
df['next_ts'] = np.where(df['B'].shift(-t).notna(),df['datetime'].shift(-t),np.datetime64('NaT'))
else:
None
对于上述代码,我得到以下错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
所需的输出如下所示:
A B datetime next_ts
10 NaN 12-03-2020 04:43:11 NaN
NaN 20 13-03-2020 04:43:11 NaN
NaN NaN 14-03-2020 04:43:11 NaN
NaN NaN 15-03-2020 04:43:11 NaN
NaN NaN 16-03-2020 04:43:11 NaN
NaN 50 17-03-2020 04:43:11 NaN
20 NaN 18-03-2020 04:43:11 19-03-2020 04:43:11
NaN 30 19-03-2020 04:43:11 NaN
NaN NaN 20-03-2020 04:43:11 NaN
30 30 21-03-2020 04:43:11 22-03-2020 04:43:11
40 NaN 22-03-2020 04:43:11 23-03-2020 04:43:11
NaN 10 23-03-2020 04:43:11 NaN
Someone please help me in achieving my logic.
相关问题 更多 >
编程相关推荐