简体   繁体   English

python pandas-在您所在的行上方添加一列的值

[英]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.

相关问题 Pandas-根据列值在一行中查找第一次出现 - Pandas- Finding first occurence in a row based on column values 熊猫-在保留列/索引值的同时向DataFrame添加缺少的日期? - Pandas- adding missing dates to DataFrame while keeping column/index values? 将多重CSV文件合并到熊猫时在列中添加文件名-Python - Adding file name in a Column while merging multible csv files to pandas- Python Python Pandas-如何解开具有两个值的数据透视表,每个值变成一个新列? - Python Pandas- how to unstack a pivot table with two values with each value becoming a new column? Pandas-将列除以另一列,条件是值是否大于0? - Pandas- Dividing a column by another column conditional on if values are greater than 0? pandas-向数据框添加一系列会导致出现NaN值 - pandas- adding a series to a dataframe causes NaN values to appear pandas-列中每个唯一字符串/组的新计算行 - pandas- new calculated row for each unique string/group in a column 熊猫-将列转换为日期时间格式时包含不必要的值 - Pandas- Unnecessay values included while converting a column into datetime format Pandas-创建一个基于列值插入新行的表? - Pandas- creating a table that inserts new rows based on column values? 用上一行中的字符串替换列中的零(Python / Pandas) - Replace zeros in a column with string from the row above (Python/Pandas)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM