Deedle C#(對於開發者的運行原理)
Deedle C#
Deedle是一個強大的資料處理和資料分析庫。 它提供整個資料框架和序列,讓您有效率地處理結構化資料框架。 Deedle 提供了用於處理缺失資料、對齊資料以及應用輔助函數的工具,其靜態成員為 ofNullables 和 ofObservations。 因其靈活性和效能,被廣泛應用於資料科學。
IronPDF是用於在 .NET 中建立和操作 PDF 文件的程式庫。 它可以幫助您從 HTML 生成 PDF、將圖像轉換為 PDF,以及從 PDF 檔案中提取內容。 IronPDF 可簡化 .NET 專案中的 PDF 任務。
在本文中,您將學習如何開始使用 Deedle for C#,使用 Visual Studio 在您的 .NET 專案中設定它,並透過自動產生的文件實現主要功能。 您將看到程式碼範例和說明,以協助您瞭解如何有效使用 Deedle,包括如何套用指定的函式。
Getting Started with Deedle C#
在 .NET 專案中設定 Deedle
首先,在 Visual Studio 中建立一個新的 C# Console Application 專案。
若要在您的 .NET 專案中使用 Deedle,您需要安裝 Deedle NuGet 套件。 在 NuGet 主控台中執行以下指令:
Install-Package Deedle
安裝完成後,您需要將 Deedle 命名空間匯入專案中:
using 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);
}
}
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
在這個範例中,您建立了一個具有整數行鍵和雙值的序列。 然後,您使用雙值的 2D 陣列建立資料框架。 您以整數為行索引,以字串為列索引。
Implementing Features of Deedle C#
處理缺失值
處理遺漏值在資料處理中至關重要。 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);
}
}
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。
資料處理
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);
}
}
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 實作了標準的框架延伸方法,讓資料分析變得簡單直接。
統計函數
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}");
}
}
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() 分別計算序列的平均值和標準差。
從 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);
}
}
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 檔案讀入資料框架,並對資料執行摘要作業。
將 Deedle 與 IronPDF 整合。
IronPDF 簡介

IronPDF 是一個功能強大的函式庫,可讓您在 .NET 應用程式中建立、處理 PDF 檔案,並從 PDF 檔案中擷取內容。 它具有高度的通用性,可以處理各種 PDF 相關工作,例如從 HTML 產生 PDF、擷取文字、合併 PDF 等。 將 IronPDF 與 Deedle 整合,對於需要從資料框架產生動態報告的資料分析和報告情境特別有用。
安裝 IronPDF
若要使用 NuGet Package Manager Console 在您的 .NET 專案中安裝 IronPDF,請新增下列指令:
Install-Package IronPdf
或者您也可以使用 NuGet Package Manager for Solutions 安裝 IronPDF。 在搜尋結果中尋找 IronPDF 在 NuGet 上的套件,選取該套件,然後按一下"安裝"按鈕。 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)
{
// 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!");
}
}
}
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!");
}
}
}
Imports System
Imports System.Linq
Imports Deedle
Imports IronPdf
Namespace DeedleIronPDFIntegration
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Set IronPDF license key
IronPdf.License.LicenseKey = "License-Key"
' Create a sample data frame from in-memory records
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 an HTML table format
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 content
Dim renderer = New ChromePdfRenderer()
Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
' Save the generated PDF to a file
pdfDocument.SaveAs("f:\DeedleReport.pdf")
Console.WriteLine("PDF report created successfully!")
End Sub
End Class
End Namespace
輸出

就是這樣! 您剛剛建立了一個功能完整的應用程式,它可以從 Deedle 取得複雜的資料,並使用 IronPDF 的 .NET PDF Library 將它轉換成格式化的 PDF 報表。 這是以專業格式傳達資料分析結果的有效方式。
結論
在本文中,我們探討了如何將 Deedle 與 IronPDF 整合,從資料框架建立動態 PDF 報表。 使用 Deedle,您可以有效地操作和分析資料,而 IronPDF 則處理最終 PDF 文件的建立和格式化。 這樣的組合讓您可以輕鬆產生專業報告,自動化從資料分析到簡報的流程。
IronPDF 提供詳細的 功能與使用說明文件,以及各種 IronPDF 程式碼範例,引導您如何開始使用並有效地使用其豐富的功能。
從 $999 開始探索 IronPDF 授權選項。 試試看,看看它能如何增強您的報告能力。
常見問題解答
Deedle C#用於什麼?
Deedle C#被用於數據處理和分析,提供工具以高效處理結構化數據框和系列。它特別在數據科學應用中實用,因其能夠管理缺失數據、對齊數據和應用函數。
如何在.NET中將Deedle與PDF生成集成?
你可以將Deedle與IronPDF整合,從數據框生成動態PDF報告。Deedle負責數據處理,而IronPDF用於格式化並生成最終的PDF報告,包括表格、圖表和統計。
如何在.NET項目中安裝Deedle?
要在.NET項目中安裝Deedle,你可以使用Visual Studio創建一個新的C#控制台應用程式,然後使用命令Install-Package Deedle安裝Deedle NuGet包,並在你的項目中包含using Deedle;。
使用Deedle從CSV文件創建數據框的過程是什麼?
要使用Deedle從CSV文件創建數據框,可以使用Frame.ReadCsv()方法。這允許你從CSV文件中加載結構化數據到數據框中進行分析和處理。
Deedle可以處理數據框中的缺失值嗎?
是的,Deedle提供了強大的支持以處理數據框中的缺失值。你可以使用像FillMissing()這樣的函數來管理和適當地填補系列或數據框中的缺失數據。
如何使用Deedle進行統計分析?
Deedle提供內建的統計函數,允許你進行數據分析,包括計算平均值、標準差以及其他統計指標,直接在數據框和系列上。
如何在.NET中從數據框生成PDF報告?
要在.NET中從數據框生成PDF報告,可以使用Deedle進行數據處理和IronPDF進行PDF生成。在使用Deedle處理完數據後,用IronPDF格式化並輸出到專業風格的PDF報告中。



