[英]python pandas- adding values of a column above the row you're on
I have a dataframe that looks like this (but longer): 我有一个看起来像这样的数据框(但更长):
OnsetTime OffsetTime OnSec OffSec RTsec TrialDur
36163 38165 36.163 38.165 0.000 2.002
39157 41152 39.157 41.152 0.605 1.995
42152 44155 42.152 44.155 0.509 2.003
45164 47153 45.164 47.153 0.503 1.989
48159 50161 48.159 50.161 0.558 2.002
I want to make a new column that would, for each row, add the values in the TrialDur column above but not including it. 我想创建一个新列,该列将为每行在上面的TrialDur列中添加值,但不包括它。 and it would need to add on .001 of a second, since TrialDur is trial duration, and I want my new column to indicate the time when a new stimulus came on the screen.
而且由于TrialDur是试用期限,因此需要加上.001秒,并且我希望新列指示屏幕上出现新刺激的时间。 so it would look like this:
所以看起来像这样:
NewVar
0
2.003
3.999
6.003
7.993
9.996
The first row would be 0 since the first stimulus started at timepoint 0. The second would be right after the first trial ended (based on the TrialDur variable), at 2.003 seconds, and so on. 由于第一个刺激在时间点0开始,所以第一行将为0。第二行将在第一次试验结束后(基于TrialDur变量)在2.003秒之后,依此类推。
How do I make a variable that adds the values above it in each row? 如何制作一个变量,将每行上面的值相加?
You can use cumsum
to compute the cumulative sum (add 0.001 before that), then shift
that column by 1, finally set the first row to be 0. 您可以使用
cumsum
来计算累加和(在此之前加0.001),然后将该列shift
1,最后将第一行设置为0。
df['NewVar'] = (df.TrialDur + 0.001).cumsum()
df.loc[df.index[-1]+1, 'NewVar'] = 0
df['NewVar'] = df.NewVar.shift(1)
df.loc[0, 'NewVar'] = 0
Because NewVar
has one more row, so I first add one empty row at the end, and I assume that the index is in numerical order. 由于
NewVar
还有一行,因此我首先在末尾添加一个空行,并假定索引按数字顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.