Python 数据帧中的筛选和赋值

Python 数据帧中的筛选和赋值,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有一个如下所示的数据帧: Date Student Subject Score 10/1 Luke English 65 10/1 Alex English 75 10/2 Luke Maths 50 10/3 Lily Maths 45 10/3 Alex Maths 90 10/4 Lily

我有一个如下所示的数据帧:

Date    Student    Subject    Score
10/1    Luke       English    65
10/1    Alex       English    75     
10/2    Luke       Maths      50     
10/3    Lily       Maths      45     
10/3    Alex       Maths      90     
10/4    Lily       English    60     
10/5    Alex       English    70     
10/6    Luke       English    55     
10/6    Luke       Maths      65     
10/7    Lily       Maths      80     
10/8    Alex       Maths      75     
10/8    Lily       English    90     
我在这里尝试完成两个任务,输出应该是dataframe中的三个新列:

1. Date of last/next test:
对于每一行,我想查看同一学生和科目的下一次考试日期。例如,第1行和第2行的“下一个测试日期”应分别为“10/6”和“10/5”

同样,对于最后一行,“上一个测试日期”的值应为“10/4”

2. Change in score:
对于本专栏,我希望返回基于该学生在该科目的最后一次考试的分数变化。例如,第6-8行的值应为'nan'、'-5'和'-10'


提前谢谢你的帮助

您可以使用
groupby
shift
来解决以下问题。使用
fillna()
替换
Nan

问题1:查找上次和下次测试日期:

In [941]: df['next_test_dt'] = df.groupby(['Student','Subject'])['Date'].shift(-1).fillna(df['Date'])

In [943]: df['last_test_dt'] = df.groupby(['Student','Subject'])['Date'].shift(1).fillna(df['Date'])

In [944]: df
Out[944]: 
    Date Student  Subject  Score next_test_dt last_test_dt
0   10/1    Luke  English     65         10/6         10/1
1   10/1    Alex  English     75         10/5         10/1
2   10/2    Luke    Maths     50         10/6         10/2
3   10/3    Lily    Maths     45         10/7         10/3
4   10/3    Alex    Maths     90         10/8         10/3
5   10/4    Lily  English     60         10/8         10/4
6   10/5    Alex  English     70         10/5         10/1
7   10/6    Luke  English     55         10/6         10/1
8   10/6    Luke    Maths     65         10/6         10/2
9   10/7    Lily    Maths     80         10/7         10/3
10  10/8    Alex    Maths     75         10/8         10/3
11  10/8    Lily  English     90         10/8         10/4
问题2:查找与上次考试分数的分数差:

In [954]: df['score_diff'] = df['Score'] - df.groupby(['Student','Subject'])['Score'].shift(1)

In [957]: df
Out[957]: 
    Date Student  Subject  Score next_test_dt last_test_dt  score_diff
0   10/1    Luke  English     65         10/6         10/1         NaN
1   10/1    Alex  English     75         10/5         10/1         NaN
2   10/2    Luke    Maths     50         10/6         10/2         NaN
3   10/3    Lily    Maths     45         10/7         10/3         NaN
4   10/3    Alex    Maths     90         10/8         10/3         NaN
5   10/4    Lily  English     60         10/8         10/4         NaN
6   10/5    Alex  English     70         10/5         10/1        -5.0
7   10/6    Luke  English     55         10/6         10/1       -10.0
8   10/6    Luke    Maths     65         10/6         10/2        15.0
9   10/7    Lily    Maths     80         10/7         10/3        35.0
10  10/8    Alex    Maths     75         10/8         10/3       -15.0
11  10/8    Lily  English     90         10/8         10/4        30.0
让我们。