跳至頁尾內容
.NET 幫助

Deedle C#(開發者使用指南)

針 C#。

Deedle 是一個功能強大的資料處理和資料分析函式庫。 它提供整個資料框架和序列,讓您有效率地處理結構化資料框架。 Deedle 提供了遺失資料、對齊資料以及套用具有靜態成員 ofNullablesofObservations 的輔助函數的工具。 因其靈活性和效能,被廣泛應用於資料科學。

IronPDF 是一個用於在 .NET 中建立和操作 PDF 文件的函式庫。 它可以幫助您從 HTML 生成 PDF、將圖像轉換為 PDF,以及從 PDF 檔案中提取內容。 IronPDF 可簡化 .NET 專案中的 PDF 任務。

在本文中,您將學習如何開始使用 Deedle for C#,使用 Visual Studio 在您的 .NET 專案中設定它,並透過自動產生的文件實現主要功能。 您將看到程式碼範例和說明,以協助您瞭解如何有效使用 Deedle,包括如何套用指定的函式。

開始使用 Deedle C#。

在 .NET 專案中設定 Deedle

首先,在 Visual Studio 中建立一個新的 C# Console Application 專案。

若要在您的 .NET 專案中使用 Deedle,您需要安裝 Deedle NuGet 套件。 在 NuGet 主控台中執行以下指令:

Install-Package Deedle

安裝完成後,您需要將 Deedle 命名空間匯入專案中:

using Deedle;
using Deedle;
$vbLabelText   $csharpLabel

基本程式碼範例

讓我們從一個基本範例開始,建立並操作資料框架。 這將有助於您瞭解 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);
    }
}
$vbLabelText   $csharpLabel

在這個範例中,您建立了一個具有整數行鍵和雙值的序列。 然後,您使用雙值的 2D 陣列建立資料框架。 您以整數為行索引,以字串為列索引。

實現 Deedle C&num 的功能;

處理缺失值

處理遺漏值在資料處理中至關重要。 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);
    }
}
$vbLabelText   $csharpLabel

本範例建立一個有缺失值的序列,並將其填入指定值。 您也可以使用靜態成員方法,例如 ofOptionalObservationsofValues 來處理更複雜的情境。

資料處理

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);
    }
}
$vbLabelText   $csharpLabel

本範例示範根據條件篩選行,並新增一列已轉換的資料。 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}");
    }
}
$vbLabelText   $csharpLabel

此 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);
    }
}
$vbLabelText   $csharpLabel

本範例將一個 CSV 檔案讀入資料框架,並對資料執行摘要作業。

將 Deedle 與 IronPDF 整合。

IronPDF 簡介

Deedle C# (How It Works For Developers):圖 1 - IronPDF for .NET:C# PDF Library

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!");
        }
    }
}
$vbLabelText   $csharpLabel

輸出

Deedle C# (How It Works For Developers):圖 2 - 使用 IronPDF 和 Deedle 產生的輸出 PDF

就是這樣! 您剛剛建立了一個功能完整的應用程式,它可以從 Deedle 取得複雜的資料,並使用 IronPDF 的 .NET PDF Library 將它轉換成格式化的 PDF 報表。 這是以專業格式傳達資料分析結果的有效方式。

結論

在本文中,我們探討了如何將 DeedleIronPDF 整合,從資料框架建立動態 PDF 報表。 使用 Deedle,您可以有效地操作和分析資料,而 IronPDF 則處理最終 PDF 文件的建立和格式化。 這樣的組合讓您可以輕鬆產生專業報告,自動化從資料分析到簡報的流程。

IronPdf 提供詳細的 功能與使用說明文件,以及各種 IronPDF 程式碼範例,引導您如何開始使用並有效地使用其豐富的功能。

探索 IronPDF 授權選項,從 $799 開始。 試試看,看看它能如何增強您的報告能力。

常見問題解答

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 報表。

Jacob Mellor,Team Iron 首席技術官
首席技術長

Jacob Mellor 是 Iron Software 的首席技術官,也是一位富有遠見的工程師,率先開發了 C# PDF 技術。作為 Iron Software 核心程式碼庫的最初開發者,他自公司成立之初便參與塑造了其產品架構,並與執行長 Cameron Rimington 一起將其發展成為一家擁有 50 多名員工、服務於 NASA、特斯拉和全球政府機構的公司。

Jacob 於 1998 年至 2001 年在曼徹斯特大學獲得土木工程一級榮譽學士學位。 1999 年,他在倫敦創辦了自己的第一家軟體公司;2005 年,他創建了自己的第一個 .NET 元件。此後,他專注於解決微軟生態系統中的複雜問題。

他的旗艦產品 IronPDF 和 IronSuite .NET 庫在全球 NuGet 上的安裝量已超過 3000 萬次,其基礎程式碼持續為全球開發者工具提供支援。憑藉 25 年的商業經驗和 41 年的程式設計專長,Jacob 始終致力於推動企業級 C#、Java 和 Python PDF 技術的創新,同時指導下一代技術領導者。