简体   繁体   中英

Get previous row value in dataset with Pandas

I'm new to Python and Pandas and I'm trying to get the value of a row in a dataset and the previous one, for example:

data["increment"] = data.apply(lambda column: foo(column.A, column.B, column.PREVIOUS_A_VALUE), axis=1)

When I said PREVIOUS_A_VALUE is where I want to pass to my function the previous value of A row.

Here is the dataset:

A  B
1  10
2  20
3  31
4  45
...

For example, when I got the 2 value in my function foo() I want to get the value 1 too.

@Serge Ballesta referenced the shift function. I would make the increment column the shifted version of A and then overwrite it with your apply:

data['increment'] = data.A.shift(-1)
data['increment'] = data.apply(lambda column: foo(column.A, column.B, column.increment), axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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