Totalling the sum of a column in a csv file

Hi There,
I want to add all the numerical entries in a particular column from a csv file and get the total.

I will need to do this for 3 different columns but I imagine once you get one its just a matter of changing column name for each of the other 2.

pm.test("Check CSV Column Totals match", function () {
    
    const head = 
        parsedBody.shift(), 
        signedPercentage = head.indexOf('Signed Percentage'), 
        signedInitialPremium = head.indexOf('Signed Initial Premium'), 
        signedEffectiveLimit = head.indexOf('Signed Effective Limit'); 
});

So these are the 3 columns I want to get the totals for.

Hey @markst33,

Are you using a CSV datafile in the runner or are you talking about something else?

This is the first line - Is this what you mean ?

const parse = require(‘csv-parse/lib/sync’), parsedBody = parse(responseBody, {relax_column_count: true});

Oh ok, so the response body is in CSV format? Are you able to provide an example of what that looks like, please?

This is what it looks like in the Postman Console

Would something like this work? First time that I tried it out so it might not work :smiley:

const parse = require('csv-parse/lib/sync'),
    parsedBody = parse(responseBody, { columns: true });

console.log(parseFloat(parsedBody[0]["Signed Percentage"]) + parseFloat(parsedBody[0]["Signed Initial Premium"]) + parseFloat(parsedBody[0]["Signed Effective Limit"]))

I may have not fully understood what you’re trying to do though. The is just grabbing those values and adding them together. :thinking:

What were you trying to do within this test:

pm.test("Check CSV Column Totals match", function () {
    
    const head = 
        parsedBody.shift(), 
        signedPercentage = head.indexOf('Signed Percentage'), 
        signedInitialPremium = head.indexOf('Signed Initial Premium'), 
        signedEffectiveLimit = head.indexOf('Signed Effective Limit'); 
});

If you’re totalling up the columns, would this work?

let SignedPercentageTotal = 0,
    SignedInitialPremiumTotal = 0,
    SignedEffectiveLimitTotal = 0;
    
_.each(parsedBody, (item) => {
    SignedPercentageTotal = SignedPercentageTotal + parseFloat(item["Signed Percentage"])
    SignedInitialPremiumTotal = SignedInitialPremiumTotal + parseFloat(item["Signed Initial Premium"])
    SignedEffectiveLimitTotal = SignedEffectiveLimitTotal + parseFloat(item["Signed Effective Limit"])
})

console.log(SignedPercentageTotal)
console.log(SignedInitialPremiumTotal)
console.log(SignedEffectiveLimitTotal)