Python 如何通过向后一天的时间戳过滤数据
这是我的筛选条件(向后一天),称为Python 如何通过向后一天的时间戳过滤数据,python,pandas,dataframe,filter,Python,Pandas,Dataframe,Filter,这是我的筛选条件(向后一天),称为df customer_id timestamp 1 2018-06-03 17:56:52 2 2018-06-03 18:42:51 这是主数据集,称为df2 transaction_id customer_id timestamp 1 1 2018-06-02 09:56:23 2 1 2018-06
df
customer_id timestamp
1 2018-06-03 17:56:52
2 2018-06-03 18:42:51
这是主数据集,称为df2
transaction_id customer_id timestamp
1 1 2018-06-02 09:56:23
2 1 2018-06-03 02:56:52
3 1 2018-06-03 12:56:52
4 2 2018-06-03 12:40:51
5 2 2018-06-03 18:40:51
6 2 2018-06-03 18:48:50
我想要的是
transaction_id customer_id timestamp
2 1 2018-06-03 02:56:52
3 1 2018-06-03 12:56:52
4 2 2018-06-03 12:40:51
5 2 2018-06-03 18:40:51
这是因为对于customer\u id=1
而言,筛选标准应从2018-06-02 17:56:52
到2018-06-03 17:56:52
及
这是因为对于customer\u id=2
而言,过滤标准应从2018-06-02 18:42:51
开始到2018-06-03 18:42:51
使用由map
创建的系列
,再减去一天,过滤:
详细信息:
print (s)
0 2018-06-03 17:56:52
1 2018-06-03 17:56:52
2 2018-06-03 17:56:52
3 2018-06-03 18:42:51
4 2018-06-03 18:42:51
5 2018-06-03 18:42:51
Name: customer_id, dtype: datetime64[ns]
使用由系列
创建的地图
,另一个用一天减去,过滤方式:
详细信息:
print (s)
0 2018-06-03 17:56:52
1 2018-06-03 17:56:52
2 2018-06-03 17:56:52
3 2018-06-03 18:42:51
4 2018-06-03 18:42:51
5 2018-06-03 18:42:51
Name: customer_id, dtype: datetime64[ns]
您可以创建新时间,然后检查时间戳是否位于
after = df2['customer_id'].map(df1.set_index('customer_id')['timestamp'])
before = after - pd.Timedelta('1 days')
df2[(df2['timestamp'] > before) & (df2['timestamp'] < after)]
transaction_id customer_id timestamp
1 2 1 2018-06-03 02:56:52
2 3 1 2018-06-03 12:56:52
3 4 2 2018-06-03 12:40:51
4 5 2 2018-06-03 18:40:51
after=df2['customer\u id'].map(df1.set\u index('customer\u id')['timestamp']))
前=后-pd.Timedelta('1天'
df2[(df2['timestamp']>之前)和(df2['timestamp']<之后)]
事务\u id客户\u id时间戳
1 2 1 2018-06-03 02:56:52
2 3 1 2018-06-03 12:56:52
3 4 2 2018-06-03 12:40:51
4 5 2 2018-06-03 18:40:51
您可以创建新时间,然后检查时间戳是否位于时间i,e之间
after = df2['customer_id'].map(df1.set_index('customer_id')['timestamp'])
before = after - pd.Timedelta('1 days')
df2[(df2['timestamp'] > before) & (df2['timestamp'] < after)]
transaction_id customer_id timestamp
1 2 1 2018-06-03 02:56:52
2 3 1 2018-06-03 12:56:52
3 4 2 2018-06-03 12:40:51
4 5 2 2018-06-03 18:40:51
after=df2['customer\u id'].map(df1.set\u index('customer\u id')['timestamp']))
前=后-pd.Timedelta('1天'
df2[(df2['timestamp']>之前)和(df2['timestamp']<之后)]
事务\u id客户\u id时间戳
1 2 1 2018-06-03 02:56:52
2 3 1 2018-06-03 12:56:52
3 4 2 2018-06-03 12:40:51
4 5 2 2018-06-03 18:40:51