What is the best way to add all row values in a dataframe-js?

158 views Asked by At

I have a dataframe like below and I would like to add all values in a row and store the sum in a new column.

var df = new dfjs.DataFrame({'col1': [1, 2, 3], 'col2': [4, 5, 6]}, ['col1', 'col2'])

So I have:

col1 col2
1     4
2     5
3     6

And I want to get:

col1 col2 col3
1    4    5
2    5    7
3    6    9

Given that dataframe contains many rows, what is the most efficient way to do that?

1

There are 1 answers

0
Tony On BEST ANSWER

I figured out that I could do it like this:

var df = df.withColumn('col3', row => row.toArray().reduce((a, b) => a + b, 0));

And to be on the safe side with the NaN values I extended it with:

var df = df.withColumn('col3', row => row.toArray().reduce((a, b) => a + (isNaN(b) ? 0 : b), 0));