简体   繁体   English

如何使用列上的尾随行在同一列上进行计算 熊猫蟒

[英]How to use trailing rows on a column for calculations on that same column | Pandas Python

I'm trying to figure out how to compare the element of the previous row of a column to a different column on the current row in a Pandas DataFrame. 我试图弄清楚如何将一列的上一行的元素与Pandas DataFrame中当前行的另一列进行比较。 For example: 例如:

data = pd.DataFrame({'a':['1','1','1','1','1'],'b':['0','0','1','0','0']})

Output: 输出:

   a   b
0  1   0
1  1   0
2  1   1
3  1   0
4  1   0

And now I want to make a new column that asks if (data['a'] + data['b']) is greater then the previous value of that same column. 现在,我想创建一个新列,询问(data ['a'] + data ['b'])是否大于同一列的先前值。 Theoretically: 从理论上讲:

data['c'] = np.where(data['a']==( the previous row value of data['a'] ),min((data['b']+( the previous row value of data['c'] )),1),data['b'])

So that I can theoretically output: 这样我就可以在理论上输出:

   a   b   c
0  1   0   0
1  1   0   0
2  1   1   1
3  1   0   1
4  1   0   1

I'm wondering how to do this because I'm trying to recreate this excel conditional statement: =IF(A70=A69,MIN((P70+Q69),1),P70) 我想知道如何执行此操作,因为我试图重新创建此excel条件语句:= IF(A70 = A69,MIN((P70 + Q69),1),P70)

where data['a'] = column A and data['b'] = column P. 其中data ['a'] =列A,data ['b'] =列P。

If anyone has any ideas on how to do this, I'd greatly appreciate your advice. 如果有人对如何执行此操作有任何想法,非常感谢您的建议。

According to your statement: 'new column that asks if (data['a'] + data['b']) is greater then the previous value of that same column' I can suggest you to solve it by this way: 根据您的说法: “新列询问(data ['a'] + data ['b'])是否大于同一列的先前值”,我建议您通过以下方式解决它:

>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame({'a':['1','1','1','1','1'],'b':['0','0','1','0','3']})
>>> df
   a  b
0  1  0
1  1  0
2  1  1
3  1  0
4  1  3
>>> df['c'] = np.where(df['a']+df['b'] > df['a'].shift(1)+df['b'].shift(1), 1, 0)
>>> df
   a  b  c
0  1  0  0
1  1  0  0
2  1  1  1
3  1  0  0
4  1  3  1

But it doesn't looking for 'previous value of that same column' . 但这并不是在寻找“同一列的先前值” If you would try to write df['c'].shift(1) in np.where() , it gonna to raise KeyError: 'c' . 如果您尝试将df['c'].shift(1) np.where() ,它将引发KeyError:'c'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM