将数据帧切成间隔以进行统计分析 | python

slice the dataframe into intervals for statistical analysis | python

我迷失了一个练习,我需要在一些日期时间点之间为每个会话聚合数据框的不同特征,以便最终数据具有以下格式:

          {'Sessions': [{'SessionId':'<Int>', 'MaxNote':'<Int>', 
            'groups': [{'groupId':'Int', 
        'Students':[{'studentId':<Int>,   'date':'datetime', 'MaxEnglishNote':'<Int>',
      'Math':'<Int>', 'Philosophy':'<Int>'}, 
            {'studentId':<Int>, 'date':'datetime', 'MaxEnglishNote':'<Int>', 
    'Math':'<Int>', 'Philosophy':'<Int>'}]},
            {'groupId':'Int', 'Students':
            [{'studentId':<Int>, 'date':'datetime', 'MaxEnglishNote':'<Int>',
 'Math':'<Int>', 'Philosophy':'<Int>'},
             {'studentId':<Int>, 'date':'datetime','MaxEnglishNote':'<Int>',
 'Math':'<Int>', 'Philosophy':'<Int>'}]}]}

我所做的是创建一个类似 json 的对象,我在其中存储了这些会话,但接下来的转换和聚合具有唯一 ID 的值似乎很困难。请注意,与唯一 StudentID 关联的所有值与 StudentID 同时发生 所以我征求了你的意见:我应该以其他形式存储会话间隔,更适合迭代行、聚合等,还是有迭代 json 类对象中的嵌套列表的解决方案? 我想要实现的最终结果是用于不同统计和 ml 任务的平面字典

所以我实际拥有的类似 json 的对象是这样的:

 [[{'date':'2013-10-09 09:00:00', 'value':'545747', 'field':'GroupeID'},
     {'date':'2013-10-09 09:00:00',  'value':'66463', 'field': 'StudentID'},
     {'date':'2013-10-09 09:00:00' , 'value':'197290' ,'field': 'Philosophy'},
    {'date':'2013-10-09 09:90:00','value':'470186' , 'field':'EnglishBegin'},
    {'date':'2013-10-09 09:00:00' , 'value':'470186' , 'field': 'EnglishEnd'},
     {'date':'2013-10-09 09:00:00' , 'value':'470186', 'field': 'EnglishMiddle'},
     {'date':'2013-10-09 09:00:00' , 'value':'181314' , 'field': 'Math'},
    {'date':'2013-10-09 09:35:00',  'value':'969427' ,'field': 'StudentID'},
    {'date':'2013-10-09 09:35:00' , 'value':'65645' , 'field':'EnglishEnd'},
     {'date':'2013-10-09 09:35:00' , 'value':'45433' , 'field':'EnglishMiddle'},
       {'date':'2013-10-09 09:35:00' , 'value':'181314' ,'field': 'Math'}
   {'date':'2013-10-09 09:35:00' , 'value':'003698' , 'field':'Philosphie'}],
   [{...},

         ......

我从带有列的数据框中获得;日期、字段、值,代码如下:

def create_interval():
    intervales=[]
    for index, row in bounds.iterrows():
        s = row['date_start']
        e = row['date_end']
        mask = (df['date'] > s) & (df['date'] < e)
        df_interval=df.loc[mask]
        intervales.append([{k:df_interval.values[i][v] for v,k in  enumerate(df_interval.columns)} for i in range(len(df_interval))])

    return intervales

因此,如果您知道我如何存储 df_interval 以获得最佳解析和分析,它可能对我有很大帮助!

您有关系数据并且想要执行聚合。如何使用具有适当模型的关系数据库将所有内容 link 放在一起并计算聚合?

在 StackExchange 上为他们找到这个问题和答案之后 https://softwareengineering.stackexchange.com/questions/235707/using-a-relational-database-vs-json-objects-for-event-activity-data

我意识到使用 pandas 而不是使用 json 对象可能是我拥有的数据类型的最佳解决方案