简体   繁体   English

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

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

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)

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]

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

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

相关问题 Python Pandas Dataframe 根据同一列中的前一行值计算新行值 - Python Pandas Dataframe calculating new row value based on previous row value within same column Python:如何填充依赖于前一个值(前一行)的Pandas列? - Python: How to populate Pandas column that depends on the previous value (previous row)? 获取 pandas dataframe 中列的先前非 NaN 值来计算 - Get previous non-NaN value of a column in a pandas dataframe to calculate 具有累积值的新 Pandas 列取决于前一行的条件 - New Pandas column with cumulative value depending on condition on the previous row Python通过减去以前的日期价格来计算新列的价值 - Python calculate new column where value is by subtracting the previous date price Python Pandas-根据上一行的值获取行 - Python pandas - get row based on previous row value 从 pandas df 获取上一行/特定列的值 - Get value of previous row / specific column from a pandas df 如何获取 pandas 列中的前一行值 - how can I get a previous row value in pandas column 如果从另一列的同一行看到新值,则重复前一行的值然后求和,然后在 Python 中重复当前行 - repeat previous row's value then sum if new values are seen from the same row of another column, then repeat current row in Python 使用Python / Pandas逐行获取列内容的值 - Get value of column content by row with Python/Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM