Python 考虑到从周三开始的几个星期,计算我的学生人数
我有这样一个数据帧:Python 考虑到从周三开始的几个星期,计算我的学生人数,python,pandas,Python,Pandas,我有这样一个数据帧: Timestamp Student Name 2020-04-01 15:05:17 Student1 2020-04-01 14:56:37 Student2 2020-04-02 14:25:16 Student1 2020-04-08 14:15:32 Student1 2020-04-09 13:41:01 Student2 2020-04-15 12:50:30 Student2 ... 我必须计算学
Timestamp Student Name
2020-04-01 15:05:17 Student1
2020-04-01 14:56:37 Student2
2020-04-02 14:25:16 Student1
2020-04-08 14:15:32 Student1
2020-04-09 13:41:01 Student2
2020-04-15 12:50:30 Student2
...
我必须计算学生每周的频率,但我的一周从周三开始。我尝试了这个,它与我想要的输出类似,但考虑到从周三开始的几周
我的学生可以在某个星期三和下个星期二之间标记他们的出席,这将被视为出席
期望输出的示例:
Week Student Name
Week of 2020-04-01 Student1 2
Student2 1
Week of 2020-04-08 Student1 1
Student2 1
Week of 2020-04-15 Student2 1
...
使用熊猫
groupby
和Goruper
from pandas.tseries.offsets import DateOffset
import pandas as pd
# groupby student name and your week ending on Tuesday and get the count
new_df = df.groupby(['Student Name', pd.Grouper(key='Timestamp', freq='W-Tue')])['Student Name'].count().reset_index(level=1)
# use DateOffset to change the dates from the week end to the week start
new_df['Timestamp'] = new_df['Timestamp'] - DateOffset(6)
Timestamp Student Name
Student Name
Student1 2020-04-01 2
Student1 2020-04-08 1
Student2 2020-04-01 1
Student2 2020-04-08 1
Student2 2020-04-15 1