Deedle C#(對於開發者的運行原理)
Deedle C
Deedle 是一個強大的資料操作和分析程式庫。 它提供完整的資料框和序列,使您能有效處理結構性資料框。 Deedle 提供處理遺失資料、對齊資料以及應用助手函式的工具,這些工具包含靜態成員 ofNullables 和 ofObservations。 由於其靈活性和性能,它在資料科學中廣泛使用。
IronPDF 是一個用於在 .NET 中建立和操作 PDF 文件的程式庫。 它能幫助您從 HTML 生成 PDF,將圖片轉換為 PDF,以及從 PDF 文件中提取內容。 IronPDF 簡化了您 .NET 專案中的 PDF 任務。
在這篇文章中,您將學習如何開始使用 C# 的 Deedle,在您的 .NET 專案中設置它並使用 Visual Studio,並使用自動生成的文件實現關鍵功能。 您將看到程式碼範例和解釋,幫助您了解如何有效地使用 Deedle,包括如何應用指定的函式。
Getting Started with Deedle C
在 .NET 專案中設置 Deedle
首先,請在 Visual Studio 中建立一個新的 C# 控制台應用程式專案。
要在您的 .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 文件的內容。 它非常通用,可以處理如生成 HTML 的 PDF、提取文字、合併 PDF 等各種 PDF 相關任務。 將 IronPDF 與 Deedle 整合特別適合於資料分析和報告場景,在那裡您需要從資料框生成動態報告。
IronPDF 的安裝
要在您的 .NET 專案中使用 NuGet 套件管理器控制台安裝 IronPDF,請新增以下命令:
Install-Package IronPdf
或者您也可以使用 NuGet 套件管理器為解決方案安裝 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)
{
// 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
Module Program
Sub Main(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 {.Name = "Robert", .Age = 30, .City = "New York"},
New With {.Name = "Johnny", .Age = 25, .City = "San Francisco"},
New With {.Name = "Charlie", .Age = 35, .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 Module
End Namespace
輸出

就是這樣! 您剛剛建立了一個完整的應用程式,將複雜的資料從 Deedle 轉換為使用 IronPDF 的 .NET PDF 程式庫 格式化的 PDF 報告。 這是一個強大的方式,可以以專業格式傳達您的資料分析結果。
結論
在這篇文章中,我們探討了如何整合 Deedle 與 IronPDF 來從資料框建立動態 PDF 報告。 使用 Deedle,您可以有效地操作和分析資料,而 IronPDF 則負責建立和格式化最終的 PDF 文件。 這種結合允許您輕鬆生成專業報告,自動化從資料分析到展示的過程。
IronPDF 提供詳細的 功能和使用文件,以及各種 IronPDF 程式碼範例,指導您如何開始並有效使用其廣泛的功能。
探索 IronPDF 的授權選項,開始於 $999。 試試看,看看它如何提升您的報告能力。
常見問題
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報告。




