将 Deedle 数据框转换为 HTML table
Convert Deedle data frame to an HTML table
我正在尝试将 Deedle 数据帧转换为 HTML table。我可以先将数据帧转换为 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;
}
当存在 DateTime
类型的列时,它会抛出 Object must implement IConvertible.
错误。但是,当列只是 strings
和 ints
.
时它会起作用
更新:我使用的不是 for 循环,而是 foreach 语句:
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
,当有一个空值
中有一些代码可以执行此操作(尽管在 F# 中)
它所做的两个关键事情与您的示例不同:
它使用 df.Rows
遍历行。这为您提供了每一行 Series<K, obj>
,其中字符串是列键, obj
是框架中的盒装值
然后它使用Series.observationsAll
得到一个键值对序列。在 C# 中,您可以使用 series.ObservationsAll
来获取 KeyValuePair<K, OptionalValue<obj>>
的序列,它为您提供密钥以及(可能丢失的)装箱值。
使用这两个,您应该能够遍历所有内容并根据需要格式化框架。
我正在尝试将 Deedle 数据帧转换为 HTML table。我可以先将数据帧转换为 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;
}
当存在 DateTime
类型的列时,它会抛出 Object must implement IConvertible.
错误。但是,当列只是 strings
和 ints
.
更新:我使用的不是 for 循环,而是 foreach 语句:
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
,当有一个空值
它所做的两个关键事情与您的示例不同:
它使用
df.Rows
遍历行。这为您提供了每一行Series<K, obj>
,其中字符串是列键,obj
是框架中的盒装值然后它使用
Series.observationsAll
得到一个键值对序列。在 C# 中,您可以使用series.ObservationsAll
来获取KeyValuePair<K, OptionalValue<obj>>
的序列,它为您提供密钥以及(可能丢失的)装箱值。
使用这两个,您应该能够遍历所有内容并根据需要格式化框架。