Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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 3.x 按位\或多个值合并数据帧和分组_Python 3.x_Pandas_Merge_Group By - Fatal编程技术网

Python 3.x 按位\或多个值合并数据帧和分组

Python 3.x 按位\或多个值合并数据帧和分组,python-3.x,pandas,merge,group-by,Python 3.x,Pandas,Merge,Group By,我有两个数据帧: 第一个包含日期间人员的模式值: access_pattern = pd.DataFrame( { "person": ['John', 'John', 'Peter', 'Charly'], "from_date": [20180101, 20180101, 20180201, 20180201], "to_date": [20180108, 20180331, 20180228, 20180228],

我有两个数据帧:

第一个包含日期间人员的模式值:

access_pattern = pd.DataFrame(
    {
        "person":    ['John',   'John',   'Peter',  'Charly'],
        "from_date": [20180101, 20180101, 20180201, 20180201],
        "to_date":   [20180108, 20180331, 20180228, 20180228],
        "pattern":   [10, 1, 11, 0]
    }
)

access_pattern["from_date"] = pd.to_datetime(access_pattern["from_date"], unit='m')
access_pattern["to_date"] = pd.to_datetime(access_pattern["to_date"], unit='m')
第二个包含每个人的实际访问:

access = pd.DataFrame(
    {
        "person": ['John', 'John', 'Peter', 'Charly'],
        "access_date": [20180107, 20180327, 20180226, 20180223]
    }
)

access["access_date"] = pd.to_datetime(access["access_date"], unit='m')
我想合并这两个数据帧以获得:

对于access dataframe中的每个访问,我想要一个按位的_或与access_模式中的person和date匹配的所有模式的结果。对于访问日期,如果它在访问模式数据帧的from_日期和to_日期之间,则匹配

预期产出:

John   20180107 11 # bitwise_or(10, 1)
John   20180327  1
Peter  20180226 11
Charly 20180223  0

您能显示预期的输出吗?添加到问题正文中。第二行
访问
,其日期不在范围内,为什么我们对两个记录都按位或按位执行?harvpan抱歉,这是我的错。我已经更正了访问模式数据框中的“截止日期”。