Search code examples
pythonpandaslambdatuplesmapping

Calculating sum of the values in data frame by mapping the corresponding elements of a tuple


I have a data frame as shown here and below [![enter image description here][1]][1]

and I want to calculate the sum of values of the cells where the following conditions are met:

Condition1:LOC IN ('8','21') AND TYPE IN ('x','y','z')

Condition2:LOC IN ('30','16') AND TYPE IN ('x','y','z','q')

For example, I want to get the following results:

Condition1: 2+3+3+5+6+8=27 Condition2: 2+2+3+6+3+1+2+2=21

And here is the the example data frame as code:

data = {'LOC': [5, 8, 16, 21,30,35],
        'x': [5, 2, 3, 5,2,2],
        'y':[6,3,1,6,2,1],
        'z':[7,3,2,8,3,4],
        'q':[8,4,2,9,6,5],
        'p':[10,5,3,1,7,3]
       }
data_frame = pd.DataFrame(data)

Solution

  • Condition 1:

    df[df.LOC.isin([8,21])][['x','y','z']].sum().sum()
    

    2:

    df[df.LOC.isin([30,16])][['x','y','z','q']].sum().sum()
    

    Note that from pandas>=2.0.0 .sum().sum() can be replaced by .sum(axis=None)

    Edit: to answer your comment, what if a key is not in the dataframe. I would use an intersection then:

    df[df.LOC.isin([30,16])][df.columns.intersection(['x','y','z','q','h'])].sum().sum()