I have a question regarding the use of conditions in any loop while manipulating on DataFrame.
For example, I have a DataFrame
df:
a b c
1 2 5
3 4 3
2 1 7
6 3 6
5 1 9
I am trying to write a loop with a condition which checks on two cols (a and b) at a time and if the value i is available in either or both column then it should take the values from column c and store it in an array.
Using which I can later perform the statistical operations like finding mean of the array.
I have written a simplified code snippet for this task:
for i in 1:5
result1 = Float64[]
result2 = Float64[]
if (df[:, :a] = i)
push!(result1, df[:, :c])
elseif (df[:, :b] = i)
push!(result2, df[:, :c])
end
unique!(result1)
unique!(result2)
result = vcat(result1, result2)
global mean_val = mean(result)
end
Here, the i value will range from 1 to 5 and for each value both the columns a and b will be checked for its existence, if the value exist then value in column c should be pushed to the respected result array.
I have tried using some other suggestions from community like:
Code Example 1:
for i in 1:5
mean_val = mean(df[:, :c] for i in ("a", "b")
end
Code Example 2:
for i in 1:5
df.row = axes(df, 1)
mean_val = mean((filter(x->x[:a] == i || x[:b] == i ,df))[:c])
end
However these do not work and return a desired output.
Please advice on my mistake in the code. Also, please do suggest if there is any document which explains about implementing multiple conditions in a statement, and accessing dataframe elements for any other operations in julia.
Thank you in advance