我正在尝试将Deedle数据框转换为HTML表格。我可以通过首先将数据帧转换为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.
的列时,会引发DateTime
错误。但是,当列只有strings
和ints
时,它才有效。
UPDATE:我使用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
答案 0 :(得分:0)
There is some code to do this (although in F#) in the FsLab Journal source code.
The two key things that it does that are different than your sample:
It uses df.Rows
to iterate over the rows. This gives you each row as Series<K, obj>
where the string is the column key and obj
is boxed value in the frame
Then it uses Series.observationsAll
to get a sequence of key value pairs. In C#, you can use series.ObservationsAll
to get a sequence of KeyValuePair<K, OptionalValue<obj>>
, which gives you key, together with (possibly missing) boxed value.
Using these two, you should be able to iterate over everything and format the frame as you need.