Iterating through all possibilities

I have 2 Dataframes, dfV and dvS

That are dataframes that look like the following:

2 rows × 9 columns (omitted printing of 2 columns)

||column|pt1|pt2|…|pt8|
| — | — | — | — |
||String|Float64|Float64|…|Float64|
|1|EastingS|10|2|…|3|
|2|NorthingS|4|4.7|…|10||

Note: The V dataframe is structured the same way, except called EastingV and NorthingV respectively

and I want to output the final dataframe with these columns, but want to make sure there are no duplicate line segments (ie 1 --> 2 is the same as 2 --> 1

||Point A |Point B| EuclideanDist(S Points)| EuclideanDist(V Points)|
| — | — | — | — |
||String|String|Float64|…|Float64|
|1|2||EuclideanDist(1s to 2s)|EucliudeanDist(1v to 2v)||

I have tried using dataframes to iterate through each column as well as the following

@inbounds for i in eachindex(dfV, dfV2)
       dfV[i] = euclidean(dfV[i], dfV[i])
   end

any help would be appreciated! I am fairly new to Julia

Welcome to the Julia community! First, it’s best to format your code output with backticks ``` like this:

julia> dfV = DataFrame(
           pt1 = rand(1.0:0.01:99.00, 10),
           pt2 = rand(-99.0:0.01:-1.0, 10)
       )
10×2 DataFrame
│ Row │ pt1     │ pt2     │
│     │ Float64 │ Float64 │
├─────┼─────────┼─────────┤
│ 1   │ 61.91   │ -67.4   │
│ 2   │ 58.66   │ -2.84   │
│ 3   │ 67.24   │ -74.39  │
│ 4   │ 88.18   │ -5.67   │
│ 5   │ 56.37   │ -61.79  │
│ 6   │ 82.63   │ -87.87  │
│ 7   │ 12.39   │ -83.12  │
│ 8   │ 20.24   │ -97.55  │
│ 9   │ 62.59   │ -42.87  │
│ 10  │ 95.64   │ -12.08  │

You should be able to copy/paste directly from your REPL this way and it’s much easier to see what’s going on. You should also check out this thread.

I’m not 100% certain as to what it is you’re trying to do but for iterating over the rows of a DataFrame you can just do:

for row in eachrow(dfV)
    # do something here...
end

If you need to do some sort of nested iteration, you can do:

for vrow in eachrow(dfV), srow in eachrow(dfS)
    # do something here...
end

It looks like you’re just trying to compute the Euclidean distance between points and I suspect there is a better way of approaching the problem. Distances.jl exports a colwise function as well as a pairwise function so I would probably check those out to see if they do what you need them to.

3 Likes

I agree this post wasnt as easy to help as I had planned! The formatting notes are very helpful, and I was having trouble abstracting the issue i was describing. I made it work with your solution though, thanks for your help and guidance! I will try to make better questions in the future!

1 Like