我有多个数据帧,它们可以有相同的时间戳(也是+-1秒),其中有毫秒。因此,当它们都在新的数据帧中时,我想过滤掉它们之间相差超过1秒的行
有没有类似于dftogether['unique'] = np.ediff1d(dftogether['DateTime']
的函数可以处理时间戳?你知道吗
我目前的解决方案是可行的,但我正在寻找一种合适的方法。
假设我有3个数据帧,df1
,df2
和df3
。对于每个数据帧,我执行以下操作:
df1['DateTime'] = df1['DateTime'].apply(lambda
x: x.strftime('%Y%d%m%H%M%S'))
df1['DateTime']= df1['DateTime'].astype(np.int64)
它把我的DateTime
变成了int
,所以我可以这样做:
dftogether= pd.concat(z, sort=True)
dftogether= dftogether.sort_values('DateTime')
dftogether['unique'] = np.ediff1d(dftogether['DateTime'], to_begin=20181211150613411) <1
dftogether= dftogether[dftogether.unique == False]
然后我把int
转换回datetime
dftogether['DateTime'] = dftogether['DateTime'].apply(lambda x: pd.to_datetime(str(x), format='%Y%d%m%H%M%S'))
我不知道如何为时间戳创建示例数据,所以我将只复制粘贴部分数据帧。你知道吗
df1型
737 2018-12-18 12:37:19.717
738 2018-12-18 12:37:21.936
739 2018-12-18 12:37:22.841
740 2018-12-18 12:37:23.144
877 2018-12-18 12:40:53.268
878 2018-12-18 12:40:56.597
879 2018-12-18 12:40:56.899
880 2018-12-18 12:40:57.300
968 2018-12-18 12:43:31.411
969 2018-12-18 12:43:36.150
970 2018-12-18 12:43:36.452
df2型
691 2018-12-18 12:35:23.612
692 2018-12-18 12:35:25.627
788 2018-12-18 12:38:33.248
789 2018-12-18 12:38:33.553
790 2018-12-18 12:38:34.759
866 2018-12-18 12:40:29.487
867 2018-12-18 12:40:31.199
868 2018-12-18 12:40:32.206
df3型
699 2018-12-18 12:35:42.452
701 2018-12-18 12:35:45.081
727 2018-12-18 12:36:47.466
730 2018-12-18 12:36:51.796
741 2018-12-18 12:37:23.448
881 2018-12-18 12:40:57.603
910 2018-12-18 12:42:02.904
971 2018-12-18 12:43:37.361
我希望dftogether
看起来像这样,但是使用时间戳而不是int
Unique DateTime
737 False 20181812123719
738 False 20181812123721
739 False 20181812123722
741 False 20181812123723
742 True 20181812123723
740 True 20181812123723
785 False 20181812123830
786 False 20181812123831
787 False 20181812123832
787 True 20181812123832
788 False 20181812123833
所以我可以把那些放在Unique == True
785 False 2018-12-18 12:38:30
786 False 2018-12-18 12:38:31
787 False 2018-12-18 12:38:32
788 False 2018-12-18 12:38:33
790 False 2018-12-18 12:38:34
812 False 2018-12-18 12:39:10
813 False 2018-12-18 12:39:11
还有一点:我在哪里可以表达我对这项新计划的看法?我可以问一个问题吗?在我看来,这真的很糟糕,它一直在向上滚动,输入/复制代码现在真的很混乱,所有的例子真的让人分心。写这个问题花了我30多分钟
我将您的df1和df2加入到df中,并创建了如下日期列表:
然后我用1行代码得到所需的输出:
要了解其工作原理,请首先检查以下输出:
希望这有帮助。干杯。你知道吗
我这样做了,你的初始列是a和b-这是你需要的吗?你知道吗
结果:
相关问题 更多 >
编程相关推荐