Most dataframe libraries are excellent at performing columnar operations, such as adding or multiplying values within a single column. However, there are times when you may want to perform such operations across multiple columns. Below is a dataframe of Apple stock values.
shape: (4, 5)
Date
Open
High
Low
Close
str
f64
f64
f64
f64
"2024-08-12"
216.070007
219.509995
215.600006
217.529999
"2024-08-13"
219.009995
221.889999
219.009995
221.270004
"2024-08-14"
220.570007
223.029999
219.699997
221.720001
"2024-08-15"
224.600006
225.350006
222.759995
224.720001
Say we want to create another column called Sum_OHLC that adds the values from the Open to Close columns.
The manual way
Here’s the manual approach to add the values across columns: