# NMath Stats User's Guide

2.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();
```

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();
```

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, "," );
```

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" );
```

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, "," );
```

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" );
```

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

Exporting to an ADO.NET DataTable

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();
```

returns a DataTable that looks like this:

```name: CenterSpace.NMath.Stats.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.Stats.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();
```

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" );
```

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();
```

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" );

```

Top

Top