我正在尝试将 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
,当有一个空值