📜  在 Julia 中对数据框的内容进行排序

📅  最后修改于: 2021-11-25 04:37:22             🧑  作者: Mango

排序是一种按排序顺序存储数据的技术。可以使用排序算法或排序函数来执行排序。对特定数据帧进行排序,然后提取其值并应用排序函数将很容易对特定数据帧的内容进行排序。 Julia 提供了各种可用于对 DataFrame 进行排序的方法。

请记住在启动 ie DataFrames 之前在以下代码的帮助下添加以下包:

using Pkg
Pkg.add("DataFrames")

排序方法

Julia 提供了一些对 DataFrame 数据进行排序的方法:

  • sort()函数:该函数返回一个排序的数组或数组的排序副本
  • sort()函数传递算法:该函数在应用算法的帮助下返回一个排序数组
  • sortperm()函数:这个函数返回一个索引列表,可以在集合上使用这些索引来生成一个排序的集合。
  • partialsortperm()函数:此函数将算法部分排序到特定范围或排列。
  • sort()函数,rev=True:该函数将数据帧的内容按降序排序。
  • sort!():这个函数传递维度,这个函数可以对DataFrame的多维数组进行排序。
  • partialsortperm():此函数返回向量的部分排列 DataFrame 列

方法一:使用sort()函数

Julia 中的sort()函数是最基本的排序方法,可用于对数据帧的数据进行排序。

方法:

  • 首先,您可以创建数据框
  • sort()函数具有向量和列需要排序的顺序等参数。
Julia
# Creating a dataframe
df1 = DataFrame(b = [:Hi, :Med, :Hi, :Low, :Hi],
               x = ["A", "E", "I", "O","A"],
               y = [6, 3, 7, 2, 1],
               z = [2, 1, 1, 2, 2])
 
# Method1
sort(df1,[:z,:y]) # sorted z then y


Julia
# Method2 Algorithm(Quicksort)
# Sorting a particular column and storing it in s
s = sort(df1.y; alg = QuickSort)
 
# Now giving the value of s to the dataframe's y header
df1.y = s
df1 # printing the sorted y


Julia
# Method3 Algorithm(PartialQuickSort)
# If we want sort a column upto a certain number
B = 3
t = sort(df1.z; alg = PartialQuickSort(B))
 
# passing the t variable in the dataframe
df1.z = t
df1


Julia
# Method4
r = df1.y
 
# returned indexes of the elements
k = sortperm(r)
 
# traversing through indices
for i in k
    println(r[i]) 
end


Julia
# Created new dataframe as df2
df2 = DataFrame(x = [11,12, 13, 10, 23],
                y = [6, 3, 7, 2, 1],
                z = [2, 1, 1, 2, 2])
# Method5
s2 = sort(df2.x; alg = InsertionSort)
 
# now update the df2.x
df2.x = s2
df2


Julia
# Method6
a = df2.y
a = a[partialsortperm(a, 1:5)]
a


Julia
# Method7
s2 = sort(df2, rev=true)
df2 = s2 #updating the whole dataframe
df2


Julia
# Method8
B = [4 3; 1 2]
sort!(B, dims = 1); B # sorting through row
sort!(B, dims = 2); B # sorting through column


方法 2:使用 Quicksort 算法进行排序

Julia 允许将算法类型传递给sort()函数以对列进行排序。 sort(dataframe.columnheader; alg=QuickSort)函数将列名和算法类型作为参数。

方法:

  • 这里, sort()函数应用于特定的列。
  • 它在 sort函数作为参数传递
  • 然后您想要对特定列进行排序的算法也作为参数传递
  • 将此函数的返回值存储在单独的变量中
  • 然后在特定列中更新

朱莉娅

# Method2 Algorithm(Quicksort)
# Sorting a particular column and storing it in s
s = sort(df1.y; alg = QuickSort)
 
# Now giving the value of s to the dataframe's y header
df1.y = s
df1 # printing the sorted y

方法 3:使用 Partial QuickSort 算法进行排序

sort(dataframe.columnheader; alg=PartialQuickSort(range))函数与 PartialQuickSort 算法一起传递,以将列排序到算法中传递的某个限制。

方法:

  • 这里, sort()函数应用于特定的列。
  • 它在 sort函数作为参数传递
  • 然后您想要对特定列进行排序的算法(PartialQuickSort)也作为参数传递
  • 将此函数的返回值存储在单独的变量中
  • 然后在特定列中更新

朱莉娅

# Method3 Algorithm(PartialQuickSort)
# If we want sort a column upto a certain number
B = 3
t = sort(df1.z; alg = PartialQuickSort(B))
 
# passing the t variable in the dataframe
df1.z = t
df1

方法四:使用sortperm()函数

sortperm()函数与列名一起传递,以对列进行排序并返回已排序列的索引。

方法:

  • 首先将要在其中应用此排序的特定列存储在单独的变量中
  • 应用sortperm()函数并将变量作为参数传递,这将返回特定列的排序索引并将返回的索引存储在单独的变量中
  • 然后在存储索引的变量中使用for循环遍历
  • 使用 for 循环打印并在存储特定列的变量中传递索引。

朱莉娅

# Method4
r = df1.y
 
# returned indexes of the elements
k = sortperm(r)
 
# traversing through indices
for i in k
    println(r[i]) 
end

方法五:使用插入排序算法进行排序

sort(dataframe.column;alg=InsertionSort)函数与 InsertionSort 算法一起传递,以将列排序到算法中传递的某个限制。

方法:

  • 创建一个新的数据框并应用 sort()函数
  • 这次使用的算法是插入排序
  • 然后您想要对特定列进行排序的算法(InsertionSort)也作为参数传递
  • 将此函数的返回值存储在单独的变量中
  • 然后在特定列中更新

朱莉娅

# Created new dataframe as df2
df2 = DataFrame(x = [11,12, 13, 10, 23],
                y = [6, 3, 7, 2, 1],
                z = [2, 1, 1, 2, 2])
# Method5
s2 = sort(df2.x; alg = InsertionSort)
 
# now update the df2.x
df2.x = s2
df2

方法六:使用partialsortperm()函数

partialsortperm(dataframe.column, range)函数是sortperm()函数的高级形式 这将返回范围内值的索引。这对列进行了部分排序。

方法:

  • 存储需要在另一个变量中排序的特定列
  • 应用partialsortperm()函数传递向量和需要排序的范围
  • 最后,我们可以在将结果传递到特定 DataFrame 的列的帮助下进行更新
  • 现在打印数据框只会打印更新的值

朱莉娅

# Method6
a = df2.y
a = a[partialsortperm(a, 1:5)]
a

方法七:降序排序

sort(dataframe,rev=True)函数与 dataframe 和 rev 变量一起传递以对列进行排序。此函数基本上反转或给出传递的列的降序。

方法:

  • 使用sort()函数按降序对数据框的特定列进行排序
  • 首先将特定列存储在变量中
  • 并应用sort()函数并将特定列的反向传递为 rev = true
  • 这现在将按降序排序
  • 最后,通过将变量传递到数据帧来更新数据帧

朱莉娅

# Method7
s2 = sort(df2, rev=true)
df2 = s2 #updating the whole dataframe
df2

      

方法八:使用sort()!函数

sort!(vector,dim)函数与数据框和维度一起传递,我们要在其中对列进行排序(维度表示 dim=1 表示行,dim=2 表示列)。

方法:

  • 现在的行或列应用的排序与用户的选择,要么排序函数
  • 按行排序是通过将向量传递给 sort!()函数的
  • 此外,我们需要传递dim=1,这意味着按行遍历
  • 此函数将按行方式打印排序
  • 现在通过仅传递 dim=2 以列方式排序来应用相同的函数。
  • 这现在将以列方式打印排序的向量。

朱莉娅

# Method8
B = [4 3; 1 2]
sort!(B, dims = 1); B # sorting through row
sort!(B, dims = 2); B # sorting through column