NMath User's Guide

TOC | Previous | Next | Index

37.2 Creating DataFrames (.NET, C#, CSharp, VB, Visual Basic, F#)

Data frames can be constructed in a variety of ways.

Creating Empty DataFrames

The default constructor creates an empty data frame with no rows or columns. Columns and rows can then be added to the new data frame.

Code Example – C#

var df = new DataFrame();



// Add some columns
df.AddColumn( new DFStringColumn( "Sex" ));  
df.AddColumn( new DFStringColumn( "AgeGroup" ));
df.AddColumn( new DFIntColumn( "Weight" ) );



// Add some rows
df.AddRow( "John Smith", "M", "Child", 45 );
df.AddRow( "Ruth Barnes", "F", "Senior", 115 );
df.AddRow( "Jane Jones", "F", "Adult", 115 );
df.AddRow( "Timmy Toddler", "M", "Child", 42 );
df.AddRow( "Betsy Young", "F", "Adult", 130 );
df.AddRow( "Arthur Smith", "M", "Senior", 142 );
df.AddRow( "Lucy Doe", "F", "Child", 30 );
df.AddRow( "Emma Allen", "F", "Child", 35 );

Code Example – VB

Dim DF As New DataFrame()



'' Add some columns
DF.AddColumn( New DFStringColumn( "Sex" ))  
DF.AddColumn( New DFStringColumn( "AgeGroup" ))
DF.AddColumn( New DFIntColumn( "Weight" ) )



'' Add some rows
DF.AddRow( "John Smith", "M", "Child", 45 )
DF.AddRow( "Ruth Barnes", "F", "Senior", 115 )
DF.AddRow( "Jane Jones", "F", "Adult", 115 )
DF.AddRow( "Timmy Toddler", "M", "Child", 42 )
DF.AddRow( "Betsy Young", "F", "Adult", 130 )
DF.AddRow( "Arthur Smith", "M", "Senior", 142 )
DF.AddRow( "Lucy Doe", "F", "Child", 30 )
DF.AddRow( "Emma Allen", "F", "Child", 35 )

NOTE—The first parameter to the AddRow() method is the row key. See Section 37.3 and Section 37.4, respectively, for more information on adding columns and rows to a data frame.

Creating DataFrames from Arrays of Columns

You can also construct and populate columns independently, then combine them into a data frame:

Code Example – C#

var col1 = new DFNumericColumn( "Col1", 1.1, 2.2, 3.3, 4.4 );
var col2 = new DFBoolColumn ( "Col2", true, true, false, true );
var col3 =
  new DFStringColumn ( "Col3", "John", "Paulo", "Sam", "Becky" );
var cols = new DFColumn[] { col1, col2, col3 };
var df = new DataFrame( cols );

Code Example – VB

Dim Col1 As New DFNumericColumn("Col1", 1.1, 2.2, 3.3, 4.4)
Dim Col2 As New DFBoolColumn("Col2", True, True, False, True)
Dim Col3 As
  New DFStringColumn("Col3", "John", "Paulo", "Sam", "Becky")
Dim Cols As DFColumn() = {Col1, Col2, Col3}
Dim DF As New DataFrame(Cols)

An InvalidArgumentException is thrown if the columns are not all of the same length.

In this case, the row keys are set to nulls; they can later be initialized using the SetRowKeys() method. Alternatively, you can pass in a collection of row keys at construction time:

Code Example – C#

var keys = new object[] { "Row1", "Row2", "Row3", "Row4" };
var df = new DataFrame( cols, keys );

Code Example – VB

Dim Keys As Object() = {"Row1", "Row2", "Row3", "Row4"}
Dim DF As New DataFrame(Cols, Keys)

Creating DataFrames from Matrices

You can construct a data frame from a DoubleMatrix and an array of column names. A new DFNumericColumn is added for each column in the matrix. For instance, this code creates a data frame from an 8 x 3 matrix:

Code Example – C#

var A = new DoubleMatrix( 8, 3, 0, 1 );
var colNames = new string[] { "A", "B", "C" };
var df = new DataFrame( A, colNames );

Code Example – VB

Dim A As New DoubleMatrix(8, 3, 0, 1)
Dim ColNames As String() = {"A", "B", "C"}
Dim DF As New DataFrame(A, ColNames)

The number of column names must match the number of columns in the matrix.

Creating DataFrames from ADO.NET Objects

You can construct a data frame from an ADO.NET DataTable. For example, assuming table is a DataTable instance:

Code Example – C#

var df = new DataFrame( table );

Code Example – VB

Dim DF As New DataFrame(Table)

In this case, the row keys are set to the default rowIndex + 1—that is, 1...n. You can also specify the row keys in various ways. This code passes in an array of row keys:

Code Example – C#

var keys = new object[] { "Row1", "Row2", "Row3", "Row4" };
var df = new DataFrame( table, keys );

Code Example – VB

Dim Keys As Object() { "Row1", "Row2", "Row3", "Row4" }
Dim DF As New DataFrame(Table, Keys)

Alternatively, you can indicate a column in the DataTable, either by column index or column name, to use for the row keys. This code uses column ID for row keys:

Code Example – C#

var df = new DataFrame( table, "ID" );

Code Example – VB

Dim DF As New DataFrame(Table, "ID")

Creating DataFrames from Strings

You can construct a data frame from a string representation. For example, if str is a tab-delimited string containing:



Key  Col1 Col2   Col3
Row1 1.1  true   A
Row2 2.2  true   B
Row3 3.3  false  A
Row4 4.4  true   C

Then you could construct a data frame like so:

Code Example – C#

var df = new DataFrame( str );

Code Example – VB

Dim DF As New DataFrame(Str)

For more control, you can also indicate:

whether the first row of data contains column headers

whether the first column of data contains row keys

the delimiter used to separate columns

whether to parse the column types, or to treat everything as string data

For example, if str is a comma-delimited string containing column headers but no row keys:



Col1,Col2,Col3
1.1,true,A
2.2,true,B
3.3,false,A
4.4,true,C

you could construct a data frame like so:

Code Example – C#

var df = new DataFrame( str, true, false, ",", true );

Code Example – VB

Dim DF As New DataFrame(Str, True, False, ",", True)

Top

Top