pandas sum particular rows conditional on values in different rows


These is solution based on your option 1 suggestion:

# Import pandas
import pandas as pd
import numpy as np

# Sample df
d = {'FieldA': [True, '', '', '', '', False, '', '', '', ''],'FieldB': [1, 2, 1, 4, 6, 5, 7, 9, 0, 1], 'FieldC': [0.3, 0.2, 0.3, 0.2, 0.2, 0.3, 0.2, 0.3, 0.2, 0.2]}
df = pd.DataFrame(data=d)

# Create temporaty column to find index distance from last True/False
t_mod = []
for i in list(df.index.values):
    t_mod.append(i%5)

df['t_mod_c'] = np.array(t_mod)

Output

 # Add missinf True/False values to FieldA based in column t_mod_c
test = []
for i in df.index.values:
    test.append(df['FieldA'].loc[i-df['t_mod_c'].loc[i]])
df.drop(['t_mod_c'], axis=1, inplace = True)
df['FieldA'] = np.array(test)
df

enter image description here

# Sum FieldB based on FieldA value
df[df['FieldA'] == True]['FieldB'].sum()

Hope it helps!
If you have ony questions let me know.
Good luck!

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top