Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.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_Dataframe_Python Datetime - Fatal编程技术网

Python 滚动两个数据框并比较一列数据

Python 滚动两个数据框并比较一列数据,python,dataframe,python-datetime,Python,Dataframe,Python Datetime,我有以下数据帧: import pandas as pd import numpy as np df_Sensor = pd.DataFrame({'ID_System_Embed': ['1000', '1000', '1000', '1003', '1004'], 'Date_Time': ['2020-10-18 12:58:05', '2020-10-18 12:58:15',

我有以下数据帧:

      import pandas as pd
      import numpy as np

      df_Sensor = pd.DataFrame({'ID_System_Embed': ['1000', '1000', '1000', '1003', '1004'], 
                      'Date_Time': ['2020-10-18 12:58:05', '2020-10-18 12:58:15',
                                    '2020-10-19 20:10:10', '2018-12-18 12:58:00', 
                                    '2015-10-25 11:00:00']})



     df_Period = pd.DataFrame({'ID_System_Embed': ['1000', '1000', '1001', '1002', '1003', '1004'],
                      'ID_Sensor': ['1', '2', '3', '4', '5', '6'], 
                      'Date_Init': ['2020-10-18 12:58:00', '2020-10-18 19:58:00',
                                    '2019-11-18 19:58:00', '2018-12-29 12:58:00',
                                    '2019-11-20 12:58:00', '2015-10-25 10:00:00'],

                      'Date_End': ['2020-10-18 16:58:00', '2020-10-19 20:58:00',
                                   '2019-11-25 12:58:00', '2018-12-18 12:58:00',
                                   '2019-11-25 12:58:00', '2015-10-25 12:00:00']})
我需要检测数据帧“df_传感器”的日期是否包含在同一ID_System_Embed(嵌入式系统标识符)的第二个数据帧(df_周期)的日期范围内

我尝试实现以下代码:

      df_Period['New_Column'] = 0

     for j in range(0, len(df_Period)):
          for i in range(0, len(df_Sensor)):


              if((df_Sensor['ID_System_Embed'].iloc[i] == df_Period['ID_System_Embed'].iloc[j]) &
                 (df_Sensor['Date_Time'].iloc[i] >= df_Period['Date_Init'].iloc[j]) &
                 (df_Sensor['Date_Time'].iloc[i] <= df_Period['Date_End'].iloc[j])):

                   df_Period['New_Column'].iloc[j] += 1       

将测向周期和测向传感器按['ID\u System\u Embed','ID\u Sensor']作为唯一键分组
然后使用appnd函数将其他日期列的值聚合为一个列表

def appnd(col):
    return [d for d in col]

df_p = df_Period.copy().groupby(['ID_System_Embed', 'ID_Sensor']).agg(appnd)
df_s = df_Sensor.copy().groupby(['ID_System_Embed']).agg(appnd)
然后连接两个数据帧(可以用0填充NaN)

将此函数应用于将结果映射到新_列的日期列

def inInterval(row):
    ctr = 0
    for d in row[2]:
        for start, end in zip(row[0], row[1]):
            if  start <= d <= end: ctr +=1
    return ctr

df['New_Column'] = df[ ['Date_Init', 'Date_End', 'Date_Time'] ].copy()\
                    .apply(lambda x: inInterval(x)  if type(x[2]) == list else 0, axis = 1)
df
defininterval(行):
ctr=0
对于第[2]行中的d:
对于开始,以zip结尾(第[0]行,第[1]行):
如果开始
df = df_p.join(df_s).fillna(value = 0)
df['New_Column'] = 0
df
def inInterval(row):
    ctr = 0
    for d in row[2]:
        for start, end in zip(row[0], row[1]):
            if  start <= d <= end: ctr +=1
    return ctr

df['New_Column'] = df[ ['Date_Init', 'Date_End', 'Date_Time'] ].copy()\
                    .apply(lambda x: inInterval(x)  if type(x[2]) == list else 0, axis = 1)
df