37.9 Reordering DataFrames (.NET, C#, CSharp, VB, Visual Basic, F#)
The DataFrame class provides method for both sorting rows, and for arbitrarily reordering rows and columns.
The SortRows() method sorts the rows in a data frame according to a given ordered array of column indices. The first index is the primarily sort column, the second index is the secondary sort column, and so forth. For instance:
Code Example – C#
df.SortRows( 3, 0, 1 );
Code Example – VB
DF.SortRows(3, 0, 1)
By default, all sorting is in ascending order.
For more control, you can also pass an array of SortingType enumerated values (Ascending or Descending):
Code Example – C#
int[] colIndices = { 3, 0, 1 };
SortingType[] sortingTypes = { SortingType.Ascending,
SortingType.Descending,
SortingType.Ascending };
df.SortRows( colIndices, sortingTypes );
Code Example – VB
Dim ColIndices As Integer() = {3, 0, 1}
Dim SortingTypes As SortingType() = {SortingType.Ascending,
SortingType.Descending,
SortingType.Ascending}
DF.SortRows(ColIndices, SortingTypes)
Finally, the SortRowsByKeys() method sorts the rows in a data frame by their row keys, in the specified order:
Code Example – C#
df.SortRowsByKeys( SortingType.Ascending );
Code Example – VB
DF.SortRowsByKeys(SortingType.Ascending)
NOTE—StatsSettings.Sorting specifies the default SortingType.
The PermuteColumns() and PermuteRows() methods enable you to arbitrarily reorder the columns and rows in a data frame, respectively. Each method takes an array of indices. The array must be same length as the number of columns or rows, and contain unique indices. In both cases:
Code Example – C#
new[ permutation[i] ] = old[ i ]
Code Example – VB
New( permutation(i) ) = Old( i )
For example, assuming df has 3 columns, this code switches the last two columns:
Code Example – C#
df.PermuteColumns( 0, 2, 1 );
Code Example – VB
DF.PermuteColumns(0, 2, 1)
Assuming df has 5 rows, this code moves the second and fourth rows to the top:
Code Example – C#
df.PermuteRows( 2, 0, 3, 1, 4 );
Code Example – VB
DF.PermuteRows(2, 0, 3, 1, 4)