# NMath User's Guide

37.12 Exporting Data from DataFrames (.NET, C#, CSharp, VB, Visual Basic, F#)

The contents of a data frame can be exported in various ways.

Exporting to a Matrix

The ToDoubleMatrix() method exports all the numeric data in a data frame to a DoubleMatrix. Non-numeric columns are ignored. For example, this code constructs a DataFrame from a DoubleMatrix, adds a column of string data, then exports the contents of the data frame to another DoubleMatrix:

Code Example – C#

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

```

```var col4 = new DFStringColumn( "D",
```
```  new String[] { "x", "x", "x", "x", "x", "x", "x", "x" } );
```
```df.AddColumn( col4 );

```

```DoubleMatrix B = df.ToDoubleMatrix();
```

Code Example – VB

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

```

```Dim Col4 As New DFStringColumn("D",
```
```  New String() {"x", "x", "x", "x", "x", "x", "x", "x"})
```
```DF.AddColumn(Col4)

```

```Dim B As DoubleMatrix = DF.ToDoubleMatrix()
```

The two matrices are equal (A == B); the string column is ignored.

Exporting to a String

The ToString() method returns a formatted string representation of a data frame:

Code Example – C#

```string str = df.ToString();
```

Code Example – VB

```Dim Str As String = DF.ToString()
```

For more control, you can also indicate:

whether to export column headers (the default is true)

whether to export row keys (the default is true)

the delimiter to use to separate columns (the default is tab-delimited)

For instance, this code exports the column headers, but not the row keys, and uses a comma delimiter:

Code Example – C#

```string str = df.ToString( true, false, "," );
```

Code Example – VB

```Dim Str As String = DF.ToString(True, False, ",")
```

Convenience methods are also provided for persisting a text representation of a data frame to a text file. Save() exports the contents of the data frame to a given filename:

Code Example – C#

```df.Save( "myData.txt" );
```

Code Example – VB

```DF.Save("myData.txt")
```

Again, you can also indicate whether to export column header or row keys, and specify the column delimiter:

Code Example – C#

```df.Save( "myData.txt", true, false, "," );
```

Code Example – VB

```DF.Save("myData.txt", True, False, ",")
```

The LaunchSaveFileDialog() method allows the end user to specify the filename. The OpenInEditor() method programmatically opens a data frame in the default text editor on the user's system. The user can then edit the contents of the data frame. Lastly, the static Load() method imports a data frame from a text file:

Code Example – C#

```DataFrame df = DataFrame.Load(  "myData.txt" );
```

Code Example – VB

```Dim DF As DataFrame = DataFrame.Load("myData.txt")
```

Again, you can indicate whether the text file includes column headers and row keys, and the delimiter used to separate the columns.

The ToDataTable() method exports the data in a data frame to an ADO.NET DataTable object. The row keys are placed in a DataColumn named DFRowKeys. Thus, this code:

Code Example – C#

```var df = new DataFrame();
```
```df.AddColumn(
```
```  new DFNumericColumn( "ids", new DoubleVector( 3, 3, -1 )));
```
```df.AddColumn(
```
```  new DFStringColumn( "names", "a", "b", "c" ));
```
```df.AddColumn(
```
```  new DFBoolColumn( "bools", true, false, true ));
```
```df.SetRowKeys( new String[] { "Row1", "Row2", "Row3" } );
```
```DataTable table = df.ToDataTable();
```

Code Example – VB

```Dim DF As New DataFrame()
```
```DF.AddColumn(
```
```  New DFNumericColumn("ids", New DoubleVector(3, 3, -1)))
```
```DF.AddColumn(
```
```  New DFStringColumn("names", "a", "b", "c"))
```
```DF.AddColumn(
```
```  New DFBoolColumn("bools", True, False, True))
```
```DF.SetRowKeys(New String() {"Row1", "Row2", "Row3"})
```
```Dim Table As DataTable = DF.ToDataTable()
```

returns a DataTable that looks like this:

```name: CenterSpace.NMath.Core.DataFrame
```
```#  DFRowKeys ids     names   bools
```
```1  Row1      3.0000  a       True
```
```2  Row2      2.0000  b       False
```
```3  Row3      1.0000  c       True
```

If no name is assigned to a data frame before ToDataTable() is called, the name of the DataTable is set to the type: CenterSpace.NMath.Core.DataFrame.

Binary and SOAP Serialization

Class DataFrame implements the ISerializable interface to control serialization and deserialization. Common Language Runtime (CLR) serialization Formatter classes call the provided GetObjectData() method at serialization time to populate a SerializationInfo object with all the data required to represent a DataFrame. For example, the BinaryFormatter class provides Serialize() and Deserialize() methods for persisting an object in binary format to a given stream. For example, this code serializes a data frame to a file:

Code Example – C#

```using System.IO;
```
```using System.Runtime.Serialization.Formatters.Binary;

```

```FileStream binStream = File.Create( "myData.dat" );
```
```var binFormatter = new BinaryFormatter();
```
```binFormatter.Serialize( binStream, df );
```
```binStream.Close();
```

Code Example – VB

```Imports System.IO
```
```Imports System.Runtime.Serialization.Formatters.Binary

```

```Dim BinStream As FileStream = File.Create("myData.dat")
```
```Dim BinFormatter As New BinaryFormatter()
```
```BinFormatter.Serialize(BinStream, DF)
```
```BinStream.Close()
```

This code restores the data frame from the file:

Code Example – C#

```binStream = File.OpenRead( "myData.dat" );
```
```DataFrame df2 = (DataFrame)binFormatter.Deserialize( binStream );
```
```binStream.Close();
```
```File.Delete( "myData.dat" );
```

Code Example – VB

```BinStream = File.OpenRead("myData.dat")
```
```Dim DF2 As DataFrame = CType(BinFormatter.Deserialize(BinStream),
DataFrame)
```
```BinStream.Close()
```
```File.Delete("myData.dat")

```

Similarly, the SoapFormatter class persists an object in SOAP format to a given stream. Thus:

Code Example – C#

```using System.IO;
```
```using System.Runtime.Serialization.Formatters.Soap;

```

```FileStream xmlStream = File.Create( "myData.xml" );
```
```var xmlFormatter = new SoapFormatter();
```
```xmlFormatter.Serialize( xmlStream, df );
```
```xmlStream.Close();
```

Code Example – VB

```Imports System.IO
```
```Imports System.Runtime.Serialization.Formatters.Soap

```

```Dim XMLStream As FileStream = File.Create("myData.xml")
```
```Dim XMLFormatter As New SoapFormatter()
```
```XMLFormatter.Serialize(XMLStream, DF)
```
```XMLStream.Close()
```

This code restores the data frame from the file:

Code Example – C#

```xmlStream = File.OpenRead( "myData.xml" );
```
```DataFrame df2 = (DataFrame)xmlFormatter.Deserialize( xmlStream )
```
```xmlStream.Close();
```
```File.Delete( "myData.xml" );
```

Code Example – VB

```XMLStream = File.OpenRead("myData.xml")
```
```Dim DF2 As DataFrame = CType(XMLFormatter.Deserialize(XMLStream),
DataFrame)
```
```XMLStream.Close()
```
```File.Delete("myData.xml")

```

Top

Top