
Deedle C#(开发人员如何使用)
Deedle C#
Deedle 是一个用于数据操作和数据分析的强大库。 它提供整个数据框架和系列,允许您高效地处理结构化数据框架。 Deedle 提供用于缺失数据、对齐数据及应用帮助函数的工具,具有静态成员 ofNullables 和 ofObservations。 它因其灵活性和性能在数据科学中被广泛使用。
IronPDF 是一个用于在.NET中创建和操作PDF文档的库。 它帮助您从 HTML 生成 PDF,将图像转换为 PDF,并从 PDF 文件中提取内容。 IronPDF 简化了您 .NET 项目中的 PDF 任务。
在本文中,您将学习如何开始使用 Deedle for C#,如何在您的 .NET 项目中使用 Visual Studio 设置它,并通过自动生成的文档实现关键功能。 您将看到代码示例和解释,以帮助您了解如何有效地使用 Deedle,包括如何应用指定函数。
Getting Started with Deedle C#
Setting Up Deedle in .NET Projects
首先,在 Visual Studio 中创建一个新的 C# 控制台应用程序项目。
要在 .NET 项目中使用 Deedle,需要安装 Deedle NuGet 包。 在 NuGet 控制台中运行以下命令:
Install-Package Deedle
安装后,您需要将 Deedle 命名空间导入到您的项目中:
using Deedle;Imports Deedle基本代码示例
让我们从一个基本示例开始来创建和操作一个数据框架。 这将帮助您理解 Deedle 的基础知识。
using System;
using Deedle;
class Program
{
static void Main()
{
// Creating a series with integer keys and double values
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 from a 2D array
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 with integer keys and double values
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 from a 2D array
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在此示例中,您创建一个具有整数行键和双精度值的系列。 然后,您使用二维数组的双精度值创建数据框架。 您使用整数为行和字符串为列进行索引。
Implementing Features of Deedle C#
Handling Missing Values
处理缺失值在数据操作中至关重要。 Deedle 提供了对缺失数据的强大支持。 您可以创建一个具有缺失值的系列,并执行操作来处理它们。
using System;
using Deedle;
class Program
{
static void Main()
{
// Creating a series with nullable doubles to represent missing values
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 (e.g., 0.0)
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()
' Creating a series with nullable doubles to represent missing values
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 (e.g., 0.0)
Dim filledSeries = series.FillMissing(0.0)
Console.WriteLine("Series after Filling Missing Values:")
Console.WriteLine(filledSeries)
End Sub
End Class此示例创建一个具有缺失值的系列,并用指定值填充值。 您还可以使用像 ofOptionalObservations 和 ofValues 这样的静态成员方法来处理更复杂的场景。
Data Manipulation
Deedle 允许您执行各种数据操作任务。 您可以过滤、转换和汇总数据框架中的数据。
using System;
using Deedle;
class Program
{
static void Main()
{
// 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("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);
// Add a new column 'C' which is the sum of columns 'A' and 'B'
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()
' 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("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)
' Add a new column 'C' which is the sum of columns 'A' and 'B'
dataFrame.AddColumn("C", dataFrame("A") + dataFrame("B"))
Console.WriteLine("Transformed Data Frame with New Column 'C':")
Console.WriteLine(dataFrame)
End Sub
End Class此示例演示了如何根据条件过滤行,并添加一个包含转换数据的新列。 Deedle 实现了标准的框架扩展方法,使得数据分析变得简单。
Statistical Functions
Deedle 提供了标准的统计函数来分析数据。 使用统计函数,您可以计算平均值、标准差和其他统计度量。
using System;
using Deedle;
class Program
{
static void Main()
{
// Creating a series with integer keys and double values
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 the mean of the series
var mean = series.Mean();
Console.WriteLine($"Mean: {mean}");
// Calculate the standard deviation of the series
var stddev = series.StdDev();
Console.WriteLine($"Standard Deviation: {stddev}");
}
}Imports System
Imports Deedle
Friend Class Program
Shared Sub Main()
' Creating a series with integer keys and double values
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 the mean of the series
Dim mean = series.Mean()
Console.WriteLine($"Mean: {mean}")
' Calculate the standard deviation of the series
Dim stddev = series.StdDev()
Console.WriteLine($"Standard Deviation: {stddev}")
End Sub
End Class此 Deedle 代码示例实现了标准统计函数 Mean() 和 StdDev(),分别计算序列的平均值和标准差。
Creating Data Frames from CSV
Deedle 让您可以轻松地从 CSV 文件中创建数据框架。 这对于加载和分析结构化数据很有帮助。
using System;
using Deedle;
class Program
{
static void Main()
{
// Load a data frame from a CSV file
var dataFrame = Frame.ReadCsv("data.csv");
Console.WriteLine("Data Frame from CSV:");
Console.WriteLine(dataFrame);
// Aggregate rows by a specified column and compute sum
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 a data frame from a CSV file
Dim dataFrame = Frame.ReadCsv("data.csv")
Console.WriteLine("Data Frame from CSV:")
Console.WriteLine(dataFrame)
' Aggregate rows by a specified column and compute sum
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此示例将 CSV 文件读取到数据框架中,并对数据执行汇总操作。
Integrating Deedle with IronPDF
IronPDF介绍

IronPDF 是一个强大的库,可以让您在 .NET 应用程序中创建、操作和提取 PDF 文件内容。 它具有很高的灵活性,可以处理各种与 PDF 相关的任务,如从 HTML 生成 PDF,提取文本,合并 PDF 等等。 将 IronPDF 与 Deedle 集成在一起对于数据分析和报告场景尤其有用,在这些场景中您需要从数据框架生成动态报告。
Installation of IronPDF
要在您的 .NET 项目中使用 NuGet 包管理器控制台安装 IronPDF,添加以下命令:
PM > Install-Package IronPdf
或者,您也可以使用 NuGet 包管理器安装 IronPDF。 在搜索结果中查找 IronPDF 包,选择它,然后点击"安装"按钮。 Visual Studio 将自动处理下载和安装。
安装完成后,可以在您的项目中使用 IronPDF。
Use Case of Merging IronPDF with 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)
{
// Set IronPDF license key
IronPdf.License.LicenseKey = "License-Key";
// Create a sample data frame from in-memory records
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 an HTML table format
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 content
var renderer = new ChromePdfRenderer();
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Save the generated PDF to a file
pdfDocument.SaveAs("f:\\DeedleReport.pdf");
Console.WriteLine("PDF report created successfully!");
}
}
}
输出

就这样! 您刚刚创建了一个能够将复杂数据从 Deedle 转换为使用 IronPDF 的 .NET PDF 库 格式化的 PDF 报告的完整功能应用程序。 这是一种强大的方式,可以以专业格式传达您的数据分析结果。
结论
在本文中,我们探讨了如何将Deedle与IronPDF集成,以便从数据框架创建动态 PDF 报告。 使用 Deedle,您可以有效地操作和分析数据,而 IronPDF 负责创建和格式化最终的 PDF 文档。 这种组合使您可以轻松生成专业报告,从数据分析到演示自动化。
IronPDF 提供详细的功能和用法文档,以及各种IronPDF 代码示例,指导您如何开始并有效地使用其广泛的功能。
探索 IronPDF 授权选项,从 $999 开始。 试一试,看看它如何增强您的报告能力。

Jacob Mellor is Chief Technology Officer at Iron Software and a visionary engineer pioneering C# PDF technology. As the original developer behind Iron Software's core codebase, he has shaped the company's product architecture since its inception, transforming it alongside CEO Cameron Rimington into a 50+ person company serving NASA, Tesla, and global government agencies.
Related Articles


