I want to add a number to all columns in a DataFrame. I am trying to use,
for i in names(df)
df.i = df.i .+ 1
end
But this is giving error as ArgumentError: column name :i not found in the data frame
Any help is appreciated. Thanks in advance.
Just write:
df .+= 1
to get what you want.
If you want to loop through columns it is also supported. Here are some examples:
for n in names(df)
df[!, n] .+= 1
end
for col in eachcol(df)
col .+= 1
end
Currently you can use:
for i in axes(df, 2)
df[i] .+= 1
end
or
for n in names(df)
df[n] .+= 1
end
However, in the future you might need to write (there is a discussion if we should change the meaning of single argument indexing):
for col in eachcol(df, false)
col .+= 1
end
or
foreach(x -> x .+= 1, eachcol(df, false))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With