本文介绍了前一行中的参考值与map或apply的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个数据帧df,我想基于上一行的值为每一行生成一个新的变量/列. df已排序,因此行的顺序有意义.

Given a dataframe df, I would like to generate a new variable/column for each row based on the values in the previous row. df is sorted so that the order of the rows is meaningful.

通常,我们可以使用mapapply,但是似乎它们都不允许访问上一行中的值.

Normally, we can use either map or apply, but it seems that neither of them allows the access to values in the previous row.

例如,给定现有行a b c,我想生成一个新列d,该列基于使用上一行c的值进行的一些计算.

For example, given existing rows a b c, I want to generate a new column d, which is based on some calculation using the value of c in the previous row.

我应该如何在大熊猫中做到这一点?

How should I do it in pandas?

推荐答案

如果只想基于上一行进行计算,则可以进行计算然后进行移位:

If you just want to do a calculation based on the previous row, you can calculate and then shift:

In [2]: df = pd.DataFrame({'a':[0,1,2], 'b':[0,10,20]})

In [3]: df
Out[3]:
   a   b
0  0   0
1  1  10
2  2  20

# a calculation based on other column
In [4]: df['c'] = df['b'] + 1

# shift the column
In [5]: df['c'] = df['c'].shift()

In [6]: df
Out[6]:
   a   b   c
0  0   0 NaN
1  1  10   1
2  2  20  11

如果要基于多行进行计算,可以查看rolling_apply函数( http://pandas.pydata.org/pandas-docs/stable/computation.html#moving-rolling-statistics-moments http://pandas.pydata.org/pandas-docs/stable /generation/pandas.rolling_apply.html#pandas.rolling_apply )

If you want to do a calculation based on multiple rows, you could look at the rolling_apply function (http://pandas.pydata.org/pandas-docs/stable/computation.html#moving-rolling-statistics-moments and http://pandas.pydata.org/pandas-docs/stable/generated/pandas.rolling_apply.html#pandas.rolling_apply)

这篇关于前一行中的参考值与map或apply的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:04