37.6 Accessing DataFrames (.NET, C#, CSharp, VB, Visual Basic, F#)
Class DataFrame provides a wide range of indexers and member functions accessing individual elements, columns, or rows in a data frame.
NOTE—For information on getting arbitrary sub-frames from a data frame, see Section 37.8.
Class DataFrame provides a two-dimensional indexing operator for getting and setting individual element values. Thus, df[i,j] always returns the ith element of the jth column:
Code Example – C#
df[3,0] = 1.0;
Code Example – VB
DF(3, 0) = 1.0
The one-dimensional indexing operator df[i] always returns the ith column:
Code Example – C#
DFNumericColumn col = df[3];
Code Example – VB
Dim Col As DFNumericColumn = DF(3)
You can also access columns by name:
Code Example – C#
DFNumericColumn col = df[ "myCol" ];
Code Example – VB
Dim Col As DFNumericColumn = DF("myCol")
Because column names are not constrained to be unique, this returns the first column with the given name, or null if a column by that name is not found.
The IndexOfColumn() method returns the index of the first column with a given name, or null if a column by that name is not found. IndicesOfColumn() returns an array of all column indices for a given column name.
You can also check whether a column of a given name exists in a data frame using the ContainsColumn() method:
Code Example – C#
if ( df.ContainsColumn( "myCol" ) )
{
// Do something here with df[ "myCol" ]
}
Code Example – VB
If (DF.ContainsColumn("myCol")) Then
'' Do something here with DF( "myCol" )
End If
Finally, the GetColumnDictionary() method returns an IDictionary of the values in a given column. For instance, this code gets a dictionary of the values in column 2:
Code Example – C#
IDictionary dict = df.GetColumnDictionary( 2 );
Code Example – VB
Dim Dict As IDictionary = DF.GetColumnDictionary(2)
The row keys are used as keys in the dictionary. Alternatively, you can specify two column indices—the first is used for the dictionary keys, the second for the dictionary values:
Code Example – C#
IDictionary dict = df.GetColumnDictionary( 0, 2 );
Code Example – VB
Dim Dict As IDictionary = DF.GetColumnDictionary(0, 2)
In this example, the elements in column 0 are used as the dictionary keys.
Because the one-dimensional indexer df[i] is already used for accessing data frame columns, class DataFrame provides GetRow() methods for accessing individual rows. Thus, GetRow( i ) returns the data in the ith row as an array of objects:
Code Example – C#
object[] rowData = df.GetRow( 3 );
Code Example – VB
Dim RowData As Object() = DF.GetRow(3)
You can also access rows by key:
Code Example – C#
object[] rowData = df.GetRow( "myKey" );
Code Example – VB
Dim RowData As Object() = DF.GetRow("myKey")
Because row keys are not constrained to be unique, this returns the first row with the given key, or null if a row with that key is not found.
The IndexOfKey() method returns the index of the first row with a given key, or null if a row with that key is not found. IndicesOfKey() returns an array of all row indices for a given key.
You can also retrieve the indices of rows with a particular value in a given column. IndexOf() returns the first row with a particular value in a column; IndicesOf() returns all rows. For instance, this code gets an array of row indices for all rows which have the value "John Doe" in column 2:
Code Example – C#
int[] rowIndices = df.IndicesOf( 2, "John Doe" );
Code Example – VB
Dim RowIndices As Integer() = DF.IndicesOf(2, "John Doe")
Lastly, the GetRowDictionary() method returns an IDictionary of the data in a given row, specified either by row index or row key. The column names are used as keys in the dictionary. Thus, this code gets a dictionary of the data in row 3:
Code Example – C#
IDictionary dict = df.GetRowDictionary( 3 );
Code Example – VB
Dim Dict As IDictionary = DF.GetRowDictionary(3)