简体   繁体   中英

Apply function in a pandas dataframe

I've figured out how apply a function to an entire column or subsection of a pandas dataframe in lieu of writing a loop that modifies each cell one by one.

Is it possible to write a function that takes cells within the dataframe as inputs when doing the above?

Eg. A function that in the current cell returns the product of the previous cell's value multiplied by the cell before that previous cell. I'm doing this line by line now in a loop and it is unsurprisingly very inefficient. I'm quite new to python.

For the case you mention (multiplying the two previous cells), you can do the following (which loops through each column, but not each cell):

import pandas as pd

a = pd.DataFrame({0:[1,2,3,4,5],1:[2,3,4,5,6],2:0,3:0})

for i in range(2,len(a)):
    a[i] = a[i-1]*a[i-2]

This will make each column in a the previous two columns multiplied together

If you want to perform this operation going down rows instead of columns, you can just transpose the dataframe (and then transpose it again after performing the loop to get it back in the original format)

EDIT

What's actually wanted is the product of the elements in the previous rows of two columns and the current rows of two columns. This can be accomplished using shift:

import pandas as pd

df= pd.DataFrame({"A": [1,2,3,4], "B": [1,2,3,4], "C": [2,3,4,5], "D": [5,5,5,5]})

df['E'] = df['A'].shift(1)*df['B'].shift(1)*df['C']*df['D']

df['E']

Produces:

0      NaN
1     15.0
2     80.0
3    225.0

这样就可以了,根据您的需要,变速可以前进和后退:

df['Column'] = df['Column'].shift(1) * df['Column'].shift(2)

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