Search code examples
pythonpandasdataframeapply

Comparing the value of a column with the previous value of a new column using Apply in Python (Pandas)


I have a dataframe with these values in column A:

df = pd.DataFrame(A,columns =['A'])

    A
0   0
1   5
2   1
3   7
4   0
5   2
6   1
7   3
8   0

I need to create a new column (called B) and populate it using next conditions:

Condition 1: If the value of A is equal to 0 then, the value of B must be 0.

Condition 2: If the value of A is not 0 then I compare its value to the previous value of B. If A is higher than the previous value of B then I take A, otherwise I take B. The result should be this:

    A   B
0   0   0
1   5   5
2   1   5
3   7   7
4   0   0
5   2   2
6   1   2
7   3   3

The dataset is huge and using loops would be too slow. I would need to solve this without using loops and the pandas “Loc” function. Anyone could help me to solve this using the Apply function? I have tried different things without success.

Thanks a lot.


Solution

  • One way to do this I guess could be the following

    def do_your_stuff(row):
        global value
        # fancy stuff here
        value = row["b"]
        [...]
    
    value = df.iloc[0]['B']
    df["C"] = df.apply(lambda row: do_your_stuff(row), axis=1)