{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[this doc on github](https://github.com/dotnet/interactive/tree/master/samples/notebooks/csharp/Samples)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "#i \"nuget:https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json\" \r\n", "#i \"nuget:https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json\" \r\n", "\r\n", "#r \"nuget:Microsoft.Data.Analysis,0.2.0\"\r\n", "#r \"nuget: XPlot.Plotly.Interactive, 4.0.6\"\r\n", "\r\n", "using Microsoft.Data.Analysis;\r\n", "using static Microsoft.DotNet.Interactive.Formatting.PocketViewTags;\r\n", "using Microsoft.DotNet.Interactive.Formatting;" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "PrimitiveDataFrameColumn dateTimes = new PrimitiveDataFrameColumn(\"DateTimes\"); // Default length is 0.\n", "PrimitiveDataFrameColumn ints = new PrimitiveDataFrameColumn(\"Ints\", 3); // Makes a column of length 3. Filled with nulls initially\n", "StringDataFrameColumn strings = new StringDataFrameColumn(\"Strings\", 3); // Makes a column of length 3. Filled with nulls initially" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "// Append 3 values to dateTimes\n", "dateTimes.Append(DateTime.Parse(\"2019/01/01\"));\n", "dateTimes.Append(DateTime.Parse(\"2019/01/01\"));\n", "dateTimes.Append(DateTime.Parse(\"2019/01/02\"));" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "DataFrame df = new DataFrame(dateTimes, ints, strings ); // This will throw if the columns are of different lengths" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "using Microsoft.AspNetCore.Html;\n", "Formatter.Register((df, writer) =>\n", "{\n", " var headers = new List();\n", " headers.Add(th(i(\"index\")));\n", " headers.AddRange(df.Columns.Select(c => (IHtmlContent) th(c.Name)));\n", " var rows = new List>();\n", " var take = 20;\n", " for (var i = 0; i < Math.Min(take, df.Rows.Count); i++)\n", " {\n", " var cells = new List();\n", " cells.Add(td(i));\n", " foreach (var obj in df.Rows[i])\n", " {\n", " cells.Add(td(obj));\n", " }\n", " rows.Add(cells);\n", " }\n", " \n", " var t = table(\n", " thead(\n", " headers),\n", " tbody(\n", " rows.Select(\n", " r => tr(r))));\n", " \n", " writer.Write(t);\n", "}, \"text/html\");" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "// To change a value directly through df\n", "df[0, 1] = 10; // 0 is the rowIndex, and 1 is the columnIndex. This sets the 0th value in the Ints columns to 10\n", "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "// Modify ints and strings columns by indexing\n", "ints[1] = 100;\n", "strings[1] = \"Foo!\";\n", "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "// Indexing can throw when types don't match.\n", "// ints[1] = \"this will throw because I am a string\"; \n", "// Info can be used to figure out the type of data in a column. \n", "df.Info()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "// Add 5 to ints through the DataFrame\n", "df[\"Ints\"].Add(5, inPlace: true);\n", "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "// We can also use binary operators. Binary operators produce a copy, so assign it back to our Ints column \n", "df[\"Ints\"] = (ints / 5) * 100;\n", "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "// Fill nulls in our columns, if any. Ints[2], Strings[0] and Strings[1] are null\n", "df[\"Ints\"].FillNulls(-1, inPlace: true);\n", "df[\"Strings\"].FillNulls(\"Bar\", inPlace: true);\n", "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "// To inspect the first row\n", "DataFrameRow row0 = df.Rows[0];\n", "row0" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "using Microsoft.AspNetCore.Html;\n", "Formatter.Register((dataFrameRow, writer) =>\n", "{\n", " var cells = new List();\n", " cells.Add(td(i));\n", " foreach (var obj in dataFrameRow)\n", " {\n", " cells.Add(td(obj));\n", " }\n", " \n", " var t = table(\n", " tbody(\n", " cells));\n", " \n", " writer.Write(t);\n", "}, \"text/html\");" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "row0" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "// Filter rows based on equality\n", "PrimitiveDataFrameColumn boolFilter = df[\"Strings\"].ElementwiseEquals(\"Bar\");\n", "boolFilter" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "DataFrame filtered = df.Filter(boolFilter);\n", "filtered" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "// Sort our dataframe using the Ints column\n", "DataFrame sorted = df.Sort(\"Ints\");\n", "sorted" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "// GroupBy \n", "GroupBy groupBy = df.GroupBy(\"DateTimes\");\n", "// Count of values in each group\n", "DataFrame groupCounts = groupBy.Count();\n", "groupCounts" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "// Alternatively find the sum of the values in each group in Ints\n", "DataFrame intsGroupSum = groupBy.Sum(\"Ints\");\n", "intsGroupSum" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "using XPlot.Plotly;\n", "using System.Linq;" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "#r \"nuget:MathNet.Numerics,4.9.0\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "using MathNet.Numerics.Distributions;\n", "double mean = 0;\n", "double stdDev = 0.1;\n", "\n", "MathNet.Numerics.Distributions.Normal normalDist = new Normal(mean, stdDev);" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "PrimitiveDataFrameColumn doubles = new PrimitiveDataFrameColumn(\"Normal Distribution\", normalDist.Samples().Take(1000));\n", "display(Chart.Plot(\n", " new Histogram()\n", " {\n", " x = doubles,\n", " nbinsx = 30\n", " }\n", "));" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "file_extension": ".cs", "mimetype": "text/x-csharp", "name": "C#", "pygments_lexer": "csharp", "version": "8.0" } }, "nbformat": 4, "nbformat_minor": 4 }