37.8 Accessing Sub-Frames (.NET, C#, CSharp, VB, Visual Basic, F#)
In addition to accessing individual elements, columns, or rows in a data frame (Section 37.6), class DataFrame provides a large number of member functions and indexers for accessing sub-frames containing any arbitrary subset of rows, columns, or both. Such methods and indexers accept Slice and Subset objects to indicate which rows and columns to return. (See Section 37.7 for more information on the Subset class.)
For example, GetColumns() returns a new data frame containing the columns indicated by a given Slice or Subset. For instance, if df has 5 columns, this code creates a new data frame containing columns 0, 4, and 5:
Code Example – C#
var colSubset = new Subset( new int[] { 0, 4, 5 } );
DataFrame subDF = df.GetColumns( colSubset );
Code Example – VB
Dim ColSubset As New Subset(New Integer() {0, 4, 5})
Dim SubDF As DataFrame = DF.GetColumns(ColSubset)
Similarly, GetRows() returns a new data frame containing the rows indicated by a given Slice or Subset. Thus, this code gets every other row in the source data frame:
Code Example – C#
var rowSubset = new Range( 0, df.Rows - 1, 2 );
DataFrame subDF = df.GetRows( rowSubset );
Code Example – VB
Dim RowSubset As New Range(0, DF.Rows - 1, 2)
Dim SubDF As DataFrame = DF.GetRows(RowSubset)
Class DataFrame also provides a wide range of indexers for accessing subframes:
Code Example – C#
this[int colIndex, Slice rowSlice]
this[int colIndex, Subset rowSubset]
this[Slice rowSlice, Slice colSlice]
this[Subset rowSubset, Subset colSubset]
this[Slice rowSlice, Subset colSubset]
this[Subset rowSubset, Slice colSlice]
Code Example – VB
Item(ColIndex As Integer, RowSlice As Slice)
Item(ColIndex As Integer, RowSubset As Subset)
Item(RowSlice As Slice, ColSlice As Slice)
Item(RowSubset As Subset, ColSlice As Slice)
Item(RowSlice As Slice, ColSubset As Subset)
Item(RowSubset As Subset, ColSlice As Slice)
These indexers can be used to return any portion of a data frame. For example, this code gets a new data frame containing columns 3-8 in reverse order, and all rows where column 0 equals Test1:
Code Example – C#
var colRange = new Range( 8, 3, -1 );
var bArray = new bool[ df.Rows ];
for ( int i = 0; i < df.Rows; i++ )
{
bArray[i] = ( df[0][i] == "Test1" );
}
var rowSubset = new Subset( bArray );
DataFrame df2 = df[ rowSubset, colRange ];
Code Example – VB
Dim ColRange As New Range(8, 3, -1)
Dim BArray() As Boolean = New Boolean(DF.Rows) {}
For I As Integer = 0 To DF.Rows - 1
BArray(I) = (DF(0)(I) = "Test1")
Next
Dim RowSubset As New Subset(BArray)
Dim DF2 As DataFrame = DF(RowSubset, ColRange)
Finally, there is the GetSubRow() method. Whereas GetRow() returns an entire row for a given row index, GetSubRow() returns the portion of the row indicated by the given column Slice or Subset:
Code Example – C#
var colSlice = new Slice( 0, 3, 1 );
object[] subRow = df.GetSubRow( 3, colSlice );
Code Example – VB
Dim ColSlice As New Slice(0, 3, 1)
Dim SubRow As Object() = DF.GetSubRow(3, ColSlice)