获取上一行的值并计算新列pandas python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22081878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:11:58  来源:igfitidea点击:

get previous row's value and calculate new column pandas python

pythonpandas

提问by Chet Meinzer

Is there a way to look back to a previous row, and calculate a new variable? so as long as the previous row is the same case what is the (previous change) - (current change), and attribute it to the previous 'ChangeEvent' in new columns?

有没有办法回顾前一行并计算一个新变量?所以只要前一行是相同的情况,什么是(先前的更改)-(当前的更改),并将其归因于新列中的前一个“ChangeEvent”?

here is my DataFrame

这是我的数据帧

>>> df
  ChangeEvent StartEvent  case              change      open  
0    Homeless   Homeless     1 2014-03-08 00:00:00 2014-02-08  
1       other   Homeless     1 2014-04-08 00:00:00 2014-02-08     
2    Homeless   Homeless     1 2014-05-08 00:00:00 2014-02-08      
3        Jail   Homeless     1 2014-06-08 00:00:00 2014-02-08     
4        Jail       Jail     2 2014-06-08 00:00:00 2014-02-08   

to add columns

添加列

Jail  Homeless case
 0    6        1
 0    30       1
 0    0        1

... and so on

... 等等

here is the df build

这是 df 构建

import pandas as pd
import datetime as DT
d = {'case' : pd.Series([1,1,1,1,2]),
'open' : pd.Series([DT.datetime(2014, 3, 2), DT.datetime(2014, 3, 2),DT.datetime(2014, 3, 2),DT.datetime(2014, 3, 2),DT.datetime(2014, 3, 2)]),
'change' : pd.Series([DT.datetime(2014, 3, 8), DT.datetime(2014, 4, 8),DT.datetime(2014, 5, 8),DT.datetime(2014, 6, 8),DT.datetime(2014, 6, 8)]),
'StartEvent' : pd.Series(['Homeless','Homeless','Homeless','Homeless','Jail']),
'ChangeEvent' : pd.Series(['Homeless','irrelivant','Homeless','Jail','Jail']),
'close' : pd.Series([DT.datetime(2015, 3, 2), DT.datetime(2015, 3, 2),DT.datetime(2015, 3, 2),DT.datetime(2015, 3, 2),DT.datetime(2015, 3, 2)])}
df=pd.DataFrame(d)

采纳答案by Andy Hayden

The way to get the previous is using the shift method:

获取前一个的方法是使用 shift 方法:

In [11]: df1.change.shift(1)
Out[11]:
0          NaT
1   2014-03-08
2   2014-04-08
3   2014-05-08
4   2014-06-08
Name: change, dtype: datetime64[ns]

Now you can subtract these columns. Note: This is with 0.13.1 (datetime stuff has had a lot of work recently, so YMMV with older versions).

现在您可以减去这些列。注意:这是 0.13.1(日期时间的东西最近有很多工作,所以用旧版本 YMMV)。

In [12]: df1.change.shift(1) - df1.change
Out[12]:
0        NaT
1   -31 days
2   -30 days
3   -31 days
4     0 days
Name: change, dtype: timedelta64[ns]

You can just apply this to each case/group:

您可以将其应用于每个案例/组:

In [13]: df.groupby('case')['change'].apply(lambda x: x.shift(1) - x)
Out[13]:
0        NaT
1   -31 days
2   -30 days
3   -31 days
4        NaT
dtype: timedelta64[ns]

回答by Julian

In addition to the previous responses, I'll add a link to solving the NaT / NaN problem, so one has uninterrupted series: How to fill NaT and NaN values separately

除了之前的回复,我会添加一个链接来解决 NaT / NaN 问题,所以有一个不间断的系列: 如何分别填充 NaT 和 NaN 值