简体   繁体   中英

Python + Pandas: DataFrame calculation without iteration

I have the following data in python/pandas DataFrame below. The target would be fill up the "Result" column:

  • if B = 0 then Result = 0
  • if B = 1 then result = A(current row) + A(next row where C=1).

It should be done without iteration.

A    B   C  Result
20   1   0    45
21   0   0    0
22   0   0    0
23   1   0    48
24   0   0    0
25   0   1    0
26   1   0    53
27   0   1    0

Thanks in advance.

df['result_2'] = np.where(df['B'] == 1,
        pd.Series(np.where(df.C == 1, df.A, np.nan)).fillna(method='bfill') + df.A,
        0
    )

print(df)

Prints:

    A  B  C  Result  result_2
0  20  1  0      45      45.0
1  21  0  0       0       0.0
2  22  0  0       0       0.0
3  23  1  0      48      48.0
4  24  0  0       0       0.0
5  25  0  1       0       0.0
6  26  1  0      53      53.0
7  27  0  1       0       0.0

For reproducibility:

df = pd.DataFrame({'A':[20,21,22,23,24,25,26,27],'B':[1,0,0,1,0,0,1,0], 'C':[0,0,0,0,0,1,0,1]})

Answer:

df['Result'] = df['A'][df['C']==1]
df.fillna(method='bfill', inplace=True)
df['Result'] = df[df['B']==1]['A'] + df[df['B']==1]['Result']
df.fillna(0, inplace=True)

Prints:

    A  B  C  Result
0  20  1  0    45.0
1  21  0  0     0.0
2  22  0  0     0.0
3  23  1  0    48.0
4  24  0  0     0.0
5  25  0  1     0.0
6  26  1  0    53.0
7  27  0  1     0.0

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