1

我正在尝试将 Deedle 数据框转换为 HTML 表。我可以通过首先将 Data Frame 转换为 C#DataTable来做到这一点,但我想直接做到这一点。我试过这段代码:

 internal static string ConvertDeedleFrameToHTML(Frame<int, string> df, string classtype, string title)
 {
     if (df.RowCount == 0) {
         return "<p">There are no results that satisfy the condition</p>";
     }
     string html = "<table border =\"1\" >";
     html += "<caption>" + title + "</caption>";
     //add header row
     html += "<tr class ='warning'>";
     for (int i = 0; i < df.ColumnCount; i++)
         html += "<td>" + df.Columns.GetKeyAt(i) + "</td>";
     html += "</tr>";
     String temp = " ";
     //add rows
     for (int i = 0; i < df.RowCount; i++)
     {
         html += "<tr>";
         for (int j = 0; j < df.ColumnCount; j++)
         {
             temp = df.GetRowAt<string>(i).Where(row => row.Key == df.Columns.GetKeyAt(j)).Observations.First().Value.ToString();
             //  temp = df.GetRowAt<int>(i).GetAt(j);
             //   temp2 = df.GetFrameData();
             html += "<td>" + temp + "</td>";
         }
         // df.GetColumnAt<int>(i);
         html += "</tr>";
     }
     html += "</table>";
     return html;
 }

Object must implement IConvertible.当有 type 的列时,它会引发错误DateTime。但是,当列只是strings和时,它可以工作ints

更新:我使用的是 foreach 语句,而不是 for 循环:

foreach (var row in df.Rows.ObservationsAll) {
         html += "<tr border =\"1\">";
         for (int j = 0; j < df.ColumnCount; j++)
         {
             if (row.Value.Value.GetAt(j) != null)
             {
                 temp = row.Value.Value.GetAt(j).ToString();
             }
          //   temp = row.Value.Value.GetAt(j).ToString() ?? " ";
             html += "<td>" + temp + "</td>";
         }
         html += "</tr>";
     }

但它给出了一个错误:OptionalValue.Value: Value is not available,当有一个空值

4

1 回答 1

0

FsLab Journal source code中有一些代码可以做到这一点(尽管在 F# 中)。

它所做的两个关键事情与您的样本不同:

  • 它用于df.Rows迭代行。这为您提供了每一行,Series<K, obj>其中字符串是列键并且obj是框架中的装箱值

  • 然后它Series.observationsAll用来获取一系列键值对。在 C# 中,您可以使用series.ObservationsAll获取序列KeyValuePair<K, OptionalValue<obj>>,它为您提供密钥,以及(可能缺少)装箱值。

使用这两个,您应该能够遍历所有内容并根据需要格式化框架。

于 2015-07-28T22:16:17.847 回答