NMath User's Guide

TOC | Previous | Next | Index

37.3 Adding and Removing Columns (.NET, C#, CSharp, VB, Visual Basic, F#)

The AddColumn() method adds a column to a data frame:

Code Example – C#

var df = new DataFrame();
var col = new DFNumericColumn( "myCol" );
df.AddColumn( col );

Code Example – VB

Dim DF As New DataFrame()
Dim Col As New DFNumericColumn("myCol")
DF.AddColumn( Col )

NOTE—The AddColumn() method raises a MismatchedSizeException if you attempt to add a column that is not the same length as any existing columns in a data frame.

You can also add all the columns from one data frame to another, optionally copying the data in the columns. For example, assuming df is a data frame, this code adds the columns of df to a new data frame and copies all the column data:

Code Example – C#

var df2 = new DataFrame();
df2.AddColumns( df, true );

Code Example – VB

Dim DF2 As New DataFrame()
DF2.AddColumns(DF, True)

Overloads of AddColumn() and AddColumns() accept ADO.NET DataColumn and DataColumnCollection instances, respectively. If the data frame already contains rows of data, you must also pass in a DataRowCollection of the same Count as the number of rows in the data frame.

InsertColumn() inserts a column at a given column index. This code adds a column in the first position:

Code Example – C#

var col = new DFStringColumn( "myCol" );
df.InsertColumn( 0, col );

Code Example – VB

Dim Col As New DFStringColumn("myCol")
DF.InsertColumn(0, Col)

RemoveColumn() removes the column at a given index:

Code Example – C#

df.RemoveColumn( 3 );

Code Example – VB

DF.RemoveColumn(3)

You can also identify a column by name:

Code Example – C#

df.RemoveColumn( "myCol" );

Code Example – VB

DF.RemoveColumn( "myCol" )

Because column names are not constrained to be unique, this method will remove all columns in the data frame with the given name.

RemoveAllColumns() removes all columns from a data frame, but preserves the existing row keys. RemoveColumns() removes the columns specified in a given subset or slice.

Clear() method removes all columns and rows from a data frame. CleanCols() returns a new data frame containing only those columns in a data frame that do not contain missing values.


Top

Top