Hello,
I have a dataframe that i sorted in order to get the maximum (of values in a column).
Now, i want to get the index corresponding to this maximum in the initial dataframe.
Iβve tried a lot of things with rownumber or other but nothing works.
Thanks in advance for your help.
There is no row index in DataFrames.
Hereβs something you can do though
julia> df = DataFrame(x = rand(10),y = rand(10))
10Γ2 DataFrame
Row β x y
β Float64 Float64
ββββββΌββββββββββββββββββββ
1 β 0.341501 0.914511
2 β 0.717087 0.28765
3 β 0.403885 0.173076
4 β 0.931974 0.269083
5 β 0.794214 0.795277
6 β 0.930986 0.2953
7 β 0.32153 0.315169
8 β 0.572526 0.641704
9 β 0.752558 0.738108
10 β 0.643852 0.992788
julia> rownum = findfirst(==(maximum(df.x)), df.x)
4
2 Likes
Alternatively you can just add a column that will serve as a row index if you prefer.
2 Likes
Just in case, the findmax()
and sortperm()
functions are quite useful too:
df = DataFrame(x = rand(10), y = rand(10))
findmax(df.x) # outputs the maximum x-value and its row number
ix = sortperm(df.x, rev=true)
ix[1] # row number with maximum x value
df.x[ix[1]] # maximum value in column x
2 Likes