Subtracting the values in one column from corresponding values in all other columns of a DataFrame

I have two DataFrames. One contains simply one column. The other contains multiple columns. The DataFrames have the same number of rows.

How do I subtract the values in the first DataFrame from the corresponding values in all columns of the second DataFrame?

Best to just use a for loop

julia> df1 = DataFrame(a = fill(100, 10));

julia> df2 = DataFrame(rand(10, 10), :auto);

julia> for n in names(df2)
           df2[:, n] .= df2[:, n] .- df1.a
       end

You can also use broadcasting, df2 .- df1[:, "a"]

4 Likes

or just df2 .-= df1.a for an in-place operation.

5 Likes