.NET 帮助

Deedle C#(开发人员如何使用它)

发布 2024年七月1日
分享:

Deedle C

是一个功能强大的数据处理和数据分析库。它提供整个数据帧和序列,使您可以高效地处理结构化数据帧。Deedle 提供了用于缺失数据、对齐数据以及使用静态成员 "ofNullables "和 "ofObservations "应用辅助函数的工具。它以其灵活性和性能在数据科学中得到广泛应用。

铁PDF 是一个用于在 .NET 中创建和处理 PDF 文档的库。它可以帮助您从 HTML 生成 PDF,将图像转换为 PDF,并从 PDF 文件中提取内容。IronPDF 简化了.NET 项目中的 PDF 任务。

在本文中,您将了解如何开始使用 Deedle for C#,如何使用 Visual Studio 在 .NET 项目中设置它,以及如何使用自动生成的文档实现关键功能。您将看到代码示例和解释,帮助您了解如何有效使用 Deedle,包括如何应用指定函数。

Deedle C# 入门

在 .NET 项目中设置 Deedle

首先,在 Visual Studio.

要在 .NET 项目中使用 Deedle,您需要安装 Deedle NuGet 软件包。在 NuGet 控制台中运行以下命令:

Install-Package Deedle

安装完成后,您需要将 Deedle 命名空间导入您的项目:

using Deedle;
using Deedle;
Imports Deedle
VB   C#

基本代码示例

让我们从创建和操作数据帧的基本示例开始。这将有助于你理解 Deedle 的基础知识。

using System;
using Deedle;
class Program
{
    static void Main()
    {
        // Creating a series
        var series = new Series<int, double>(new[] { 1, 2, 3 }, new[] { 3.5, 4.2, 5.1 });
        Console.WriteLine("Series:");
        Console.WriteLine(series);

        // Creating a data frame
        var rowIndex = new[] { 1, 2, 3 };
        var colIndex = new[] { "A", "B" };
        var data = new double[,] { { 1.0, 3.5 }, { 2.0, 4.2 }, { 3.0, 5.1 } };
        var dataFrame = Frame.FromArray2D(data)
                             .IndexRowsWith(rowIndex)
                             .IndexColumnsWith(colIndex);
        Console.WriteLine("Data Frame:");
        Console.WriteLine(dataFrame);
    }
}
using System;
using Deedle;
class Program
{
    static void Main()
    {
        // Creating a series
        var series = new Series<int, double>(new[] { 1, 2, 3 }, new[] { 3.5, 4.2, 5.1 });
        Console.WriteLine("Series:");
        Console.WriteLine(series);

        // Creating a data frame
        var rowIndex = new[] { 1, 2, 3 };
        var colIndex = new[] { "A", "B" };
        var data = new double[,] { { 1.0, 3.5 }, { 2.0, 4.2 }, { 3.0, 5.1 } };
        var dataFrame = Frame.FromArray2D(data)
                             .IndexRowsWith(rowIndex)
                             .IndexColumnsWith(colIndex);
        Console.WriteLine("Data Frame:");
        Console.WriteLine(dataFrame);
    }
}
Imports System
Imports Deedle
Friend Class Program
	Shared Sub Main()
		' Creating a series
		Dim series As New Series(Of Integer, Double)( { 1, 2, 3 }, { 3.5, 4.2, 5.1 })
		Console.WriteLine("Series:")
		Console.WriteLine(series)

		' Creating a data frame
		Dim rowIndex = { 1, 2, 3 }
		Dim colIndex = { "A", "B" }
		Dim data = New Double(, ) {
			{ 1.0, 3.5 },
			{ 2.0, 4.2 },
			{ 3.0, 5.1 }
		}
		Dim dataFrame = Frame.FromArray2D(data).IndexRowsWith(rowIndex).IndexColumnsWith(colIndex)
		Console.WriteLine("Data Frame:")
		Console.WriteLine(dataFrame)
	End Sub
End Class
VB   C#

在此示例中,您将创建一个具有整数行键和双数值的序列。然后,使用包含 double 值的二维数组创建数据帧。行的索引为整数,列的索引为字符串。

实现 Deedle C# 的功能

处理缺失值

处理缺失值在数据处理中至关重要。Deedle 为缺失数据提供了强大的支持。您可以创建具有缺失值的序列,并执行操作来处理它们。

using System;
using Deedle;
class Program
{
    static void Main()
    {
        var series = new Series<int, double?>(
            new[] { 75, 8, 47, 5 },
            new double?[] { 75.0, null, 47.0, 5.0 }
        );
        Console.WriteLine("Original Series with Missing Values:");
        Console.WriteLine(series);

        // Fill missing values with a specified value
        var filledSeries = series.FillMissing(0.0);
        Console.WriteLine("Series after Filling Missing Values:");
        Console.WriteLine(filledSeries);
    }
}
using System;
using Deedle;
class Program
{
    static void Main()
    {
        var series = new Series<int, double?>(
            new[] { 75, 8, 47, 5 },
            new double?[] { 75.0, null, 47.0, 5.0 }
        );
        Console.WriteLine("Original Series with Missing Values:");
        Console.WriteLine(series);

        // Fill missing values with a specified value
        var filledSeries = series.FillMissing(0.0);
        Console.WriteLine("Series after Filling Missing Values:");
        Console.WriteLine(filledSeries);
    }
}
Imports System
Imports Deedle
Friend Class Program
	Shared Sub Main()
		Dim series As New Series(Of Integer, Double?)( { 75, 8, 47, 5 }, New Double?() { 75.0, Nothing, 47.0, 5.0 })
		Console.WriteLine("Original Series with Missing Values:")
		Console.WriteLine(series)

		' Fill missing values with a specified value
		Dim filledSeries = series.FillMissing(0.0)
		Console.WriteLine("Series after Filling Missing Values:")
		Console.WriteLine(filledSeries)
	End Sub
End Class
VB   C#

此示例创建了一个缺失值序列,并用指定值进行了填充。您还可以使用 ofOptionalObservationsofValues 等静态成员方法来处理更复杂的情况。

数据操作

Deedle 允许你执行各种数据处理任务。您可以过滤、转换和汇总数据帧中的数据。

using System;
using Deedle;
class Program
{
    static void Main()
    {
        var rowIndex = new[] { 1, 2, 3 };
        var colIndex = new[] { "A", "B" };
        var data = new double[,] { { 1.0, 3.5 }, { 2.0, 4.2 }, { 3.0, 5.1 } };
        var dataFrame = Frame.FromArray2D(data)
                             .IndexRowsWith(rowIndex)
                             .IndexColumnsWith(colIndex);
        Console.WriteLine("Original Data Frame:");
        Console.WriteLine(dataFrame);

        // Filter rows where column 'A' is greater than 1.5
        var filteredFrame = dataFrame.Where(row => row.Value.GetAs<double>("A") > 1.5);
        Console.WriteLine("Filtered Data Frame:");
        Console.WriteLine(filteredFrame);

        dataFrame.AddColumn("C", dataFrame["A"] + dataFrame["B"]);
        Console.WriteLine("Transformed Data Frame with New Column 'C':");
        Console.WriteLine(dataFrame);
    }
}
using System;
using Deedle;
class Program
{
    static void Main()
    {
        var rowIndex = new[] { 1, 2, 3 };
        var colIndex = new[] { "A", "B" };
        var data = new double[,] { { 1.0, 3.5 }, { 2.0, 4.2 }, { 3.0, 5.1 } };
        var dataFrame = Frame.FromArray2D(data)
                             .IndexRowsWith(rowIndex)
                             .IndexColumnsWith(colIndex);
        Console.WriteLine("Original Data Frame:");
        Console.WriteLine(dataFrame);

        // Filter rows where column 'A' is greater than 1.5
        var filteredFrame = dataFrame.Where(row => row.Value.GetAs<double>("A") > 1.5);
        Console.WriteLine("Filtered Data Frame:");
        Console.WriteLine(filteredFrame);

        dataFrame.AddColumn("C", dataFrame["A"] + dataFrame["B"]);
        Console.WriteLine("Transformed Data Frame with New Column 'C':");
        Console.WriteLine(dataFrame);
    }
}
Imports System
Imports Deedle
Friend Class Program
	Shared Sub Main()
		Dim rowIndex = { 1, 2, 3 }
		Dim colIndex = { "A", "B" }
		Dim data = New Double(, ) {
			{ 1.0, 3.5 },
			{ 2.0, 4.2 },
			{ 3.0, 5.1 }
		}
		Dim dataFrame = Frame.FromArray2D(data).IndexRowsWith(rowIndex).IndexColumnsWith(colIndex)
		Console.WriteLine("Original Data Frame:")
		Console.WriteLine(dataFrame)

		' Filter rows where column 'A' is greater than 1.5
		Dim filteredFrame = dataFrame.Where(Function(row) row.Value.GetAs(Of Double)("A") > 1.5)
		Console.WriteLine("Filtered Data Frame:")
		Console.WriteLine(filteredFrame)

		dataFrame.AddColumn("C", dataFrame("A") + dataFrame("B"))
		Console.WriteLine("Transformed Data Frame with New Column 'C':")
		Console.WriteLine(dataFrame)
	End Sub
End Class
VB   C#

本示例演示了根据条件过滤行,并添加一列新的转换数据。Deedle 实现了标准的框架扩展方法,使数据分析变得简单明了。

统计功能

Deedle 提供标准统计函数来分析数据。使用统计函数,您可以计算平均值、标准偏差和其他统计量。

using System;
using Deedle;
class Program
{
    static void Main()
    {
        var series = new Series<int, double>(
            new[] { 1, 2, 3, 4 },
            new[] { 1.0, 2.0, 3.0, 4.0 }
        );
        Console.WriteLine("Series:");
        Console.WriteLine(series);

        // Calculate mean
        var mean = series.Mean();
        Console.WriteLine($"Mean: {mean}");

        // Calculate standard deviation
        var stddev = series.StdDev();
        Console.WriteLine($"Standard Deviation: {stddev}");
    }
}
using System;
using Deedle;
class Program
{
    static void Main()
    {
        var series = new Series<int, double>(
            new[] { 1, 2, 3, 4 },
            new[] { 1.0, 2.0, 3.0, 4.0 }
        );
        Console.WriteLine("Series:");
        Console.WriteLine(series);

        // Calculate mean
        var mean = series.Mean();
        Console.WriteLine($"Mean: {mean}");

        // Calculate standard deviation
        var stddev = series.StdDev();
        Console.WriteLine($"Standard Deviation: {stddev}");
    }
}
Imports System
Imports Deedle
Friend Class Program
	Shared Sub Main()
		Dim series As New Series(Of Integer, Double)( { 1, 2, 3, 4 }, { 1.0, 2.0, 3.0, 4.0 })
		Console.WriteLine("Series:")
		Console.WriteLine(series)

		' Calculate mean
		Dim mean = series.Mean()
		Console.WriteLine($"Mean: {mean}")

		' Calculate standard deviation
		Dim stddev = series.StdDev()
		Console.WriteLine($"Standard Deviation: {stddev}")
	End Sub
End Class
VB   C#

此 Deedle 代码示例实现了标准统计函数 Mean()和StdDev()分别计算序列的平均值和标准差。

从 CSV 创建数据框

Deedle 允许你轻松地从 CSV 文件创建数据帧。这有助于加载和分析结构化数据。

using System;
using Deedle;
class Program
{
    static void Main()
    {
        // Load data frame from CSV file
        var dataFrame = Frame.ReadCsv("data.csv");
        Console.WriteLine("Data Frame from CSV:");
        Console.WriteLine(dataFrame);

        var summary = dataFrame.AggregateRowsBy<string, double>(
            new[] { "ColumnName" }, // rowKeys
            null, // columnKeys, you can pass null if not required
            v => v.Sum() // aggFunc
        );
        Console.WriteLine("Summary of Data Frame:");
        Console.WriteLine(summary);
    }
}
using System;
using Deedle;
class Program
{
    static void Main()
    {
        // Load data frame from CSV file
        var dataFrame = Frame.ReadCsv("data.csv");
        Console.WriteLine("Data Frame from CSV:");
        Console.WriteLine(dataFrame);

        var summary = dataFrame.AggregateRowsBy<string, double>(
            new[] { "ColumnName" }, // rowKeys
            null, // columnKeys, you can pass null if not required
            v => v.Sum() // aggFunc
        );
        Console.WriteLine("Summary of Data Frame:");
        Console.WriteLine(summary);
    }
}
Imports System
Imports Deedle
Friend Class Program
	Shared Sub Main()
		' Load data frame from CSV file
		Dim dataFrame = Frame.ReadCsv("data.csv")
		Console.WriteLine("Data Frame from CSV:")
		Console.WriteLine(dataFrame)

		Dim summary = dataFrame.AggregateRowsBy(Of String, Double)( { "ColumnName" }, Nothing, Function(v) v.Sum())
		Console.WriteLine("Summary of Data Frame:")
		Console.WriteLine(summary)
	End Sub
End Class
VB   C#

本例将 CSV 文件读入数据帧,并对数据执行汇总操作。

将 Deedle 与 IronPDF 集成

IronPDF 简介

Deedle C# (如何为开发人员服务):图 1 - IronPDF for .NET:C# PDF 库

IronPDF 是一个功能强大的库,可让您在 .NET 应用程序中创建、处理 PDF 文件并从中提取内容。它具有很强的通用性,可以处理各种与 PDF 相关的任务,如生成 HTML 创建的 PDF 文件提取文本、合并 PDF 等。将 IronPDF 与 Deedle 集成,对于需要从数据帧生成动态报告的数据分析和报告方案特别有用。

安装 IronPDF

要使用 NuGet Package Manager Console 在 .NET 项目中安装 IronPDF,请添加以下命令:

Install-Package IronPdf

或者也可以使用 NuGet Package Manager for Solutions 安装 IronPDF。查找 IronPDF 软件包 在搜索结果中选择它,然后点击 "安装 "按钮。Visual Studio 会自动处理下载和安装。

安装完成后,您就可以在项目中使用 IronPDF 了。

IronPDF 与 Deedle 的合并使用案例

想象一下,您有一个数据框架,其中包含一些统计数据,您想将这些数据以 PDF 报告的形式呈现出来。Deedle 可以处理数据操作和分析部分,而 IronPDF 可用于格式化和生成最终报告。例如,您可以生成包含表格、图表和描述性统计数据的 PDF,使数据易于共享和展示。

用例代码示例

下面是一个完整的代码示例,演示如何将 Deedle 与 IronPDF 集成。我们将从 Deedle 数据框架创建一个简单的报告,并使用 IronPDF 生成 PDF。

using System;
using System.Linq;
using Deedle;
using IronPdf;

namespace DeedleIronPDFIntegration
{
    class Program
    {
        static void Main(string[] args)
        {
            IronPdf.License.LicenseKey = "License-Key";
            // Create a sample data frame
            var data = new[]
            {
                new { Name = "Robert", Age = 30, City = "New York" },
                new { Name = "Johnny", Age = 25, City = "San Francisco" },
                new { Name = "Charlie", Age = 35, City = "Los Angeles" }
            };
            var frame = Frame.FromRecords(data);

            // Convert the data frame to HTML table
            var htmlTable = "<table border='1' cellpadding='5' cellspacing='0'><thead><tr><th>Name</th><th>Age</th><th>City</th></tr></thead><tbody>" +
                            string.Join("", frame.Rows.Select(row =>
                                $"<tr><td>{row.Value.GetAs<string>("Name")}</td><td>{row.Value.GetAs<int>("Age")}</td><td>{row.Value.GetAs<string>("City")}</td></tr>")
                            ) +
                            "</tbody></table>";

            // Wrap the HTML table in basic HTML structure with CSS styling
            var htmlContent = $@"
            <html>
            <head>
                <style>
                    table {{
                        width: 100%;
                        border-collapse: collapse;
                    }}
                    th, td {{
                        border: 1px solid black;
                        padding: 8px;
                        text-align: left;
                    }}
                    th {{
                        background-color: #f2f2f2;
                    }}
                </style>
            </head>
            <body>
                {htmlTable}
            </body>
            </html>";

            // Create a PDF from the HTML
            var renderer = new ChromePdfRenderer();
            var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);

            // Save the PDF to file
            pdfDocument.SaveAs("f:\\DeedleReport.pdf");
            Console.WriteLine("PDF report created successfully!");
        }
    }
}
using System;
using System.Linq;
using Deedle;
using IronPdf;

namespace DeedleIronPDFIntegration
{
    class Program
    {
        static void Main(string[] args)
        {
            IronPdf.License.LicenseKey = "License-Key";
            // Create a sample data frame
            var data = new[]
            {
                new { Name = "Robert", Age = 30, City = "New York" },
                new { Name = "Johnny", Age = 25, City = "San Francisco" },
                new { Name = "Charlie", Age = 35, City = "Los Angeles" }
            };
            var frame = Frame.FromRecords(data);

            // Convert the data frame to HTML table
            var htmlTable = "<table border='1' cellpadding='5' cellspacing='0'><thead><tr><th>Name</th><th>Age</th><th>City</th></tr></thead><tbody>" +
                            string.Join("", frame.Rows.Select(row =>
                                $"<tr><td>{row.Value.GetAs<string>("Name")}</td><td>{row.Value.GetAs<int>("Age")}</td><td>{row.Value.GetAs<string>("City")}</td></tr>")
                            ) +
                            "</tbody></table>";

            // Wrap the HTML table in basic HTML structure with CSS styling
            var htmlContent = $@"
            <html>
            <head>
                <style>
                    table {{
                        width: 100%;
                        border-collapse: collapse;
                    }}
                    th, td {{
                        border: 1px solid black;
                        padding: 8px;
                        text-align: left;
                    }}
                    th {{
                        background-color: #f2f2f2;
                    }}
                </style>
            </head>
            <body>
                {htmlTable}
            </body>
            </html>";

            // Create a PDF from the HTML
            var renderer = new ChromePdfRenderer();
            var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);

            // Save the PDF to file
            pdfDocument.SaveAs("f:\\DeedleReport.pdf");
            Console.WriteLine("PDF report created successfully!");
        }
    }
}
Imports System
Imports System.Linq
Imports Deedle
Imports IronPdf

Namespace DeedleIronPDFIntegration
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			IronPdf.License.LicenseKey = "License-Key"
			' Create a sample data frame
			Dim data = {
				New With {
					Key .Name = "Robert",
					Key .Age = 30,
					Key .City = "New York"
				},
				New With {
					Key .Name = "Johnny",
					Key .Age = 25,
					Key .City = "San Francisco"
				},
				New With {
					Key .Name = "Charlie",
					Key .Age = 35,
					Key .City = "Los Angeles"
				}
			}
			Dim frame = Frame.FromRecords(data)

			' Convert the data frame to HTML table
			Dim htmlTable = "<table border='1' cellpadding='5' cellspacing='0'><thead><tr><th>Name</th><th>Age</th><th>City</th></tr></thead><tbody>" & String.Join("", frame.Rows.Select(Function(row) $"<tr><td>{row.Value.GetAs(Of String)("Name")}</td><td>{row.Value.GetAs(Of Integer)("Age")}</td><td>{row.Value.GetAs(Of String)("City")}</td></tr>")) & "</tbody></table>"

			' Wrap the HTML table in basic HTML structure with CSS styling
			Dim htmlContent = $"
            <html>
            <head>
                <style>
                    table {{
                        width: 100%;
                        border-collapse: collapse;
                    }}
                    th, td {{
                        border: 1px solid black;
                        padding: 8px;
                        text-align: left;
                    }}
                    th {{
                        background-color: #f2f2f2;
                    }}
                </style>
            </head>
            <body>
                {htmlTable}
            </body>
            </html>"

			' Create a PDF from the HTML
			Dim renderer = New ChromePdfRenderer()
			Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent)

			' Save the PDF to file
			pdfDocument.SaveAs("f:\DeedleReport.pdf")
			Console.WriteLine("PDF report created successfully!")
		End Sub
	End Class
End Namespace
VB   C#

输出

Deedle C#(如何为开发人员工作):图 2 - 使用 IronPDF 和 Deedle 生成的输出 PDF

就是这样! 您刚刚创建了一个功能齐全的应用程序,它可以从以下设备获取复杂的数据 Deedle 并使用 IronPDF.这是以专业格式交流数据分析结果的有力方式。

结论

在本文中,我们探讨了如何集成 DeedleIronPDF 从数据帧创建动态 PDF 报告。使用 Deedle,您可以高效地操作和分析数据,而 IronPDF 则处理最终 PDF 文档的创建和格式化。通过这种组合,您可以轻松生成专业报告,实现从数据分析到演示的流程自动化。

IronPDF 提供详细的 文献资料 与各种 代码示例 指导您如何开始并有效使用其丰富的功能。

IronPDF 许可 749 美元起。试一试,看看它能如何增强你的报告能力。

< 前一页
Dottrace .NET Core(它是如何为开发人员工作的)
下一步 >
C# 析构器(开发者如何使用)

准备开始了吗? 版本: 2024.9 刚刚发布

免费NuGet下载 总下载量: 10,731,156 查看许可证 >