# Issorted for Dataframe rows

**URL:** <https://discourse.julialang.org/t/issorted-for-dataframe-rows/46251>\
**Category:** Performance\
**Created:** [September 8, 2020, 9:10am UTC](https://discourse.julialang.org/t/issorted-for-dataframe-rows/46251 "2020-09-08T09:10:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [September 8, 2020, 9:10am UTC](https://discourse.julialang.org/t/issorted-for-dataframe-rows/46251/1 "2020-09-08T09:10:43Z")

</div>

I have a dataframe with a few dozen columns.  
For each row I want to ensure that a subset of the columns (let’s say the relevant columns are named x1 to x6) is sorted

I was wondering whether there is a faster approach than what I drafted below.

```julia
#Discourse MWE check if dataframe columns are sorted 
using DataFrames
using BenchmarkTools

#generate some data 
n=20_000_000;
#the relevant columns are integers
mat=trunc.(Int,90 .* rand(n,6));
#note : my actual data has additional columns (-> I need to work with a dataframe)
df=DataFrame(mat);

#sort columns
#my data is generally sorted. But I want to ensure this is the case (i.e. identify the dataframes where the data is not sorted).

#I do not care about the performance of this function
function mysort!(df)
    cols=propertynames(df)
    for i=1:size(df,1)
        df[i,:] .= sort([df[i,col] for col in cols])
    end
    return nothing
end

function myIsSorted(m::Array)
    @inbounds for i=1:size(m,1)
        if !(m[i,6]>=m[i,5]>=m[i,4]>=m[i,3]>=m[i,2]>=m[i,1])
            return false 
        end
    end
    return true 
end

function myIsSorted(df)
    m=convert(Array{Int64,2},df)
    return myIsSorted(m)
end

#sort data 
mysort!(df) #I do not care about the performance of this function

@btime myIsSorted($df) #0.37s, 915MB for 20m rows

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 8, 2020, 9:49am UTC](https://discourse.julialang.org/t/issorted-for-dataframe-rows/46251/2 "2020-09-08T09:49:41Z")

</div>

How about just

```julia
cols_to_checked = [:x1, :x2, :x3, :x4, :x5, :x6]

all(col->issorted(df[!, col]), cols_to_checked)

```

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [September 8, 2020, 11:57am UTC](https://discourse.julialang.org/t/issorted-for-dataframe-rows/46251/3 "2020-09-08T11:57:19Z")

</div>

Thanks. This is fast, but it is **not** what I want.  
In my case I want every row `df[i,cols_to_checked]` to be sorted.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 8, 2020, 12:07pm UTC](https://discourse.julialang.org/t/issorted-for-dataframe-rows/46251/4 "2020-09-08T12:07:30Z")

</div>

Can u give examples that illustrate Ur idea. Maybe I am not getting what u r doing

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 8, 2020, 12:09pm UTC](https://discourse.julialang.org/t/issorted-for-dataframe-rows/46251/5 "2020-09-08T12:09:20Z")

</div>

> [@bernhard](#):
>
> every row `df[i,cols_to_checked]` to be sorted

More precise language needed. Each row to be sorted meaning within each row x1 \<=x2\<=x3 etc?

Or row1\<=row2\<= row3 if u restrict row to those 6 columns

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [September 8, 2020, 12:11pm UTC](https://discourse.julialang.org/t/issorted-for-dataframe-rows/46251/6 "2020-09-08T12:11:30Z")

</div>

> [@bernhard](#):
>
> ```julia
> function myIsSorted(m::Array)
> @inbounds for i=1:size(m,1)
> if !(m[i,6]>=m[i,5]>=m[i,4]>=m[i,3]>=m[i,2]>=m[i,1])
> return false 
> end
> end
> return true 
> end
> 
> function myIsSorted(df)
> m=convert(Array{Int64,2},df)
> return myIsSorted(m)
> end
> 
> ```

please consider the above code from my MWE from the first post. This should illustrate what I am trying to check. You should be able to run the MWE.
