Reputation: 123
Is there any way to obtain the index of a row and the row object while iterating through a df in Julia? If not, even a way to find the index of a row would be great, as I could simply use eachrow()
and then find the iterated row's index, though I imagine this is less likely considering that naming a df's index isn't really supported in Julia.
For context, I need the index in order to find the first occurrence of 0 in each column of a df so that I can replace it with another value (say, 99). To do this, I want to iterate through all the rows of each column in a df. e.g. for df = DataFrame(a = [1, 0, 2], b = [0, 1, 0], c = [0, 0, 4]) the code:
for col_index in ["a", "b", "c"]
for row in eachrow(select(df, :i))
if row[1] == 0
df[row.index, :i] = 99
break
end
end
end
would turn df into
1 99 99
99 1 0
2 0 4
Sorry if this is a silly question, though I could not find anything to the effect of either of my goals online.
Upvotes: 3
Views: 2966
Reputation: 69949
rownumber
function to get the row number in the data frame from which the DataFrameRow
was taken.DataFrameRow
is mutable you can write your loop just as:for col_index in ["a", "b", "c"]
for row in eachrow(df)
if row[col_index] == 0
row[col_index] = 99
break
end
end
end
for col_index in ["a", "b", "c"]
col = df[!, col_index]
loc = findfirst(==(0), col)
isnothing(loc) || (col[loc] = 99)
end
Upvotes: 1
Reputation: 123
You can get something which is good enough by using enumerate(eachrow(df))
h/t @Antonello.
From the toy code I wrote in the question, this looks like:
for col_index in ["a", "b", "c"]
for (row_index, row) in enumerate(eachrow(select(df, :col_index)))
if row[1] == 0
df[row_index, :col_index] = 99
break
end
end
end
Upvotes: 0