To include the new column, simply assign the value(s) to that new column.
Example:
df[:SinX] = sin(df[:X])
head(df)
X SinX
1 0.0 0.0
2 0.06346651825433926 0.0634239196565645
3 0.12693303650867852 0.12659245357374926
4 0.19039955476301776 0.1892512443604102
5 0.25386607301735703 0.2511479871810792
6 0.3173325912716963 0.31203344569848707
Update: The form used to index the Dataframe and calculate the sine in the response, are obsolete from version >= 1.0.
The correct form of the command to insert the new column is:
df[:SinX] = sin.(df[:, :X])
The dot between the function sin
and parentheses indicate that it is a vector operation, and the index [:X]
indicates a search for all elements (:
) column X
.
Upshot:
head(df)
6×2 DataFrame
│ Row │ X │ SinX │
│ │ Float64 │ Float64 │
├─────┼───────────┼───────────┤
│ 1 │ 0.0 │ 0.0 │
│ 2 │ 0.0634665 │ 0.0634239 │
│ 3 │ 0.126933 │ 0.126592 │
│ 4 │ 0.1904 │ 0.189251 │
│ 5 │ 0.253866 │ 0.251148 │
│ 6 │ 0.317333 │ 0.312033 │
For more details, see the manual (in English): DataFrames.jl
Direct attribution runs through the Dataarray
df[:X]
only once, in the same way as themap
. Although the code generated by the two forms is very similar (@code_lowered()
), direct allocation is more efficient thanmap
(for the example of the question, about 790 times faster and 450 times lower memory consumption), as it avoids checks and type conversions (to view these problems:@code_warntype(map((x) -> sin(x), df[:X]))
)– Gomiero
in this case yes, because you are using only one variable, my idea was to give an option to those who have to map something derived from two fields.
– pmargreff