37.4 Adding and Removing Rows (.NET, C#, CSharp, VB, Visual Basic, F#)
The AddRow() method adds a row of data to a data frame. The first parameter is the row key; subsequent parameters are the row data. For example:
Code Example – C#
var df = new DataFrame();
df.AddColumn( new DFStringColumn( "Col1" ));
df.AddColumn( new DFNumericColumn( "Col2" ) );
df.AddColumn( new DFNumericColumn( "Col3" ) );
df.AddRow( 1546, "Test1", 1.5445, 667.87 );
Code Example – VB
Dim DF As New DataFrame()
DF.AddColumn( New DFStringColumn( "Col1" ))
DF.AddColumn( New DFNumericColumn( "Col2" ) )
DF.AddColumn( New DFNumericColumn( "Col3" ) )
DF.AddRow( 1546, "Test1", 1.5445, 667.87 )
NOTE—The AddRow() method raises a MismatchedSizeException if the number of row elements does not match the number of columns in the data frame.
This example uses 1546 as an integer row key, perhaps representing some sort of ID. Row keys can be any object, and need not be unique.
Additional overloads of AddRow() accept data in various collections other than an array of objects. One overload takes an ICollection. For instance:
Code Example – C#
var myQ = new Queue();
myQ.Enqueue( "Hello" );
myQ.Enqueue( 47.0 );
myQ.Enqueue( -0.34 );
df.AddRow( "Row1", myQ );
Code Example – VB
Dim MyQ As New Queue()
MyQ.Enqueue( "Hello" )
MyQ.Enqueue( 47.0 )
MyQ.Enqueue( -0.34 )
DF.AddRow( "Row1", MyQ )
Another overload accepts an IDictionary in which the keys are the column names and the values are the row data:
Code Example – C#
var df = new DataFrame();
df.AddColumn( new DFNumericColumn( "V1" ) );
df.AddColumn( new DFBoolColumn( "V2" ) );
df.AddColumn( new DFStringColumn( "V3" ) );
var myHT = new Hashtable();
myHT.Add( "V1", 3.14 );
myHT.Add( "V3", "Hello");
myHT.Add( "V2", true );
df.AddRow( "Row1", myHT );
Code Example – VB
Dim DF As New DataFrame()
DF.AddColumn( New DFNumericColumn( "V1" ) )
DF.AddColumn( New DFBoolColumn( "V2" ) )
df.AddColumn( New DFStringColumn( "V3" ) )
Dim MyHT As New Hashtable()
MyHT.Add("V1", 3.14)
MyHT.Add("V3", "Hello")
MyHT.Add("V2", True)
DF.AddRow("Row1", MyHT)
If all of the columns in your data frame are numeric, you can add a row as a DoubleVector:
Code Example – C#
var v = new DoubleVector( 10, 0, 1 );
df.AddRow( "myKey", v );
Code Example – VB
Dim V As New DoubleVector(10, 0, 1)
DF.AddRow("myKey", V)
Other overloads of AddRow() and AddRows() accept ADO.NET DataRow and DataRowCollection instances, respectively.
InsertRow() inserts a row at a given row index. For example, this code inserts a row into the second position:
Code Example – C#
var df = new DataFrame();
df.AddColumn( new DFNumericColumn( "Col1" ) );
df.AddColumn( new DFNumericColumn( "Col2" ) );
df.AddColumn( new DFNumericColumn( "Col3" ) );
df.AddRow( "Row1", 2.5, 0.0, 3.4 );
df.AddRow( "Row2", 3.14, -.5, -.33 );
df.AddRow( "Row3", 0.1, 55.34, 12.02 );
df.AddRow( "Row4", 3.14, -33.2, 7.22 );
var myRow = object[] { 5.5, 9.05, -6.11 };
df.InsertRow( 1, "Row1a", myRow );
Code Example – VB
Dim DF As New DataFrame()
DF.AddColumn(New DFNumericColumn("Col1"))
DF.AddColumn(New DFNumericColumn("Col2"))
DF.AddColumn(New DFNumericColumn("Col3"))
DF.AddRow("Row1", 2.5, 0.0, 3.4)
DF.AddRow("Row2", 3.14, -0.5, -0.33)
DF.AddRow("Row3", 0.1, 55.34, 12.02)
DF.AddRow("Row4", 3.14, -33.2, 7.22)
Dim MyRow As Object() = {5.5, 9.05, -6.11}
DF.InsertRow(1, "Row1a", MyRow)
Again, overloads are provided for adding row data in various collection types.
RemoveRow() removes the row at a given index:
Code Example – C#
df.RemoveRow( 0 );
Code Example – VB
DF.RemoveRow(0)
You can also identify a row by key:
Code Example – C#
df.RemoveRow( "Row3" );
Code Example – VB
DF.RemoveRow("Row3")
Because row keys are not constrained to be unique, this method will remove all rows in the data frame with the given key.
RemoveAllRows() removes all rows from a data frame, but preserves the existing columns. RemoveRows() removes the rows specified in a given subset or slice.
Clear() method removes all rows and columns from a data frame. CleanRows() returns a new data frame containing only those rows in a data frame that do not contain missing values.
Unlike column names which are fixed at construction time, row keys can be changed at any time. The SetRowKey() method sets the key for a given row to a given value. Remember that row keys can be any object:
Code Example – C#
df.SetRowKey( 0, 1.14 );
df.SetRowKey( 1, "John Doe" );
df.SetRowKey( 2, true );
Code Example – VB
DF.SetRowKey(0, 1.14)
DF.SetRowKey(1, "John Doe")
DF.SetRowKey(2, True)
SetRowKeys() accepts a collection of row keys, and raises a MismatchedSizeException if if the number of elements in the collection does not equal the number of rows in this data frame:
Code Example – C#
object[] keys = { "Subject1", "Subject2", "Subject3" };
df.SetRowKeys( keys );
Code Example – VB
Dim Keys As Object() = {"Subject1", "Subject2", "Subject3"}
DF.SetRowKeys(Keys)
Finally, IndexRowKeys() resets the row keys for all rows to rowIndex + 1; that is, 1...n.