我有一个时间序列数据框和一个数组,如下所示
>> DataFrame
datetime value
...
0 2019-09-17 23:55:00 210.38
1 2019-09-17 23:56:00 210.50
2 2019-09-17 23:57:00 210.51
3 2019-09-17 23:58:00 210.40
4 2019-09-17 23:59:00 210.41
...
>>timestamp array
[Timestamp('2019-09-17 00:05:00'),
Timestamp('2019-09-17 00:16:00'),
Timestamp('2019-09-17 01:21:00'),
Timestamp('2019-09-17 01:26:00'),
Timestamp('2019-09-17 01:31:00'),
Timestamp('2019-09-17 01:56:00'),
Timestamp('2019-09-17 02:06:00'),
Timestamp('2019-09-17 02:11:00'),
Timestamp('2019-09-17 13:36:00'),
Timestamp('2019-09-17 13:41:00'),
Timestamp('2019-09-17 13:46:00'),
Timestamp('2019-09-17 14:00:00'),
Timestamp('2019-09-17 14:26:00')]
我想合并这两个对象,并且任何行的值都是“datetime”,与数组中的时间戳值相同,新列的值将等于 1,其他列的值等于 0。
然后,我想在图表上绘制垂直虚线,或者只是在数组中的每个时间绘制一个突出显示的值点。
请帮忙!谢谢
最佳答案
我认为你需要:
#if necessary
#df['datetime'] = pd.to_datetime(df['datetime'])
df['new'] = df['datetime'].isin(arr).astype(int)
或者:
df['new'] = np.where(df['datetime'].isin(arr), 1, 0)
关于python - 如何将数据帧与数组时间戳合并并根据数组的条件进行绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59679522/