Reputation: 1297
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. 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. 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.
How do I make a variable that adds the values above it in each row?
Upvotes: 1
Views: 1807
Reputation: 6725
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.
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.
Upvotes: 2