感谢您阅读本文并为您提供帮助。完成测试中的最后一个问题后,我想分析学生的行为。我正在熊猫数据帧中对此进行分析。
目前,我除其他外有下表的前两列,但是,我想创建一个新列并根据第一次击中单个学生的最大值(问题序列号)来分配值。我想创建第三列,但我不知道如何。
有人可以帮我吗?
我希望我提供了所有必要的细节。否则请随时与我联系。我知道我可以使用.idxmax()找到最大值的索引,但不能针对每个学生以及在此之前和之后如何分配值。
学生卡 | 问题序列号 | 在最后一项之前还是之后? |
---|---|---|
1个 | 1个 | 之前或最后一个项目 |
1个 | 2个 | 之前或最后一个项目 |
1个 | 3 | 之前或最后一个项目 |
1个 | 1个 | 在最后一个项目之后 |
1个 | 2个 | 在最后一个项目之后 |
2个 | 1个 | 之前或最后一个项目 |
2个 | 2个 | 之前或最后一个项目 |
2个 | 3 | 之前或最后一个项目 |
2个 | 4 | 之前或最后一个项目 |
2个 | 1个 | 在最后一个项目之后 |
2个 | 2个 | 在最后一个项目之后 |
我们可以groupby
在idxmax
这里使用。然后将索引与具有最高值的索引进行比较:
m = df.index > df.groupby('Student ID')['Question sequence #'].transform("idxmax")
df['before or after last item?'] = np.where(
m,
"after last item",
"Before or last item"
)
Student ID Question sequence # before or after last item?
0 1 1 Before or last item
1 1 2 Before or last item
2 1 3 Before or last item
3 1 1 after last item
4 1 2 after last item
5 2 1 Before or last item
6 2 2 Before or last item
7 2 3 Before or last item
8 2 4 Before or last item
9 2 1 after last item
10 2 2 after last item
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句