我有这两个 Dataframe :
import pandas as pd
from pandas import Timestamp
df_1 = pd.DataFrame({'id': {0: 'A',
1: 'A',
2: 'B',
3: 'C',
4: 'C'},
'IdOrder': {0: 1, 1: 2, 2: 1, 3: 1, 4: 2},
'TrackDateTime': {0: Timestamp('2020-01-21 23:28:35'),
1: Timestamp('2020-01-28 17:12:15'),
2: Timestamp('2020-01-07 12:41:48'),
3: Timestamp('2020-01-01 22:13:44'),
4: Timestamp('2020-01-01 22:49:53')}})
df_1
df_2 = pd.DataFrame({'id': {0: 'A',
1: 'B',
2: 'C',
3: 'D',
4: 'E'},
'InitialDate': {0: Timestamp('2020-01-21 23:28:35'),
1: Timestamp('2020-01-07 12:41:48'),
2: Timestamp('2020-01-01 22:13:44'),
3: Timestamp('2020-01-02 15:45:10'),
4: Timestamp('2020-01-02 22:21:36')},
'EndDate': {0: Timestamp('2020-01-28 00:00:00'),
1: Timestamp('2020-01-08 00:00:00'),
2: Timestamp('2020-01-03 00:00:00'),
3: Timestamp('2020-01-06 00:00:00'),
4: Timestamp('2020-04-10 00:00:00')}})
df_2
我希望用df_2
中的InitialDate
和EndDate
将df_1
中的每个id
括起来,这将给予以下预期输出:
还请考虑:
IdOrder=0
用于InitialDate
,IdOrder=-1
用于EndDate
- 我想保持时间粒度达到秒(输出图像不显示,因为excel格式)
我还没能找到解决这个问题的办法。有什么建议吗?:)
1条答案
按热度按时间kr98yfug1#
您可以使用
melt
、concat
和自定义排序:输出: