Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何通过向后一天的时间戳过滤数据_Python_Pandas_Dataframe_Filter - Fatal编程技术网

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