1
+ using System ;
2
+ using System . Text ;
3
+ using PandasNet ;
4
+ using System . Linq ;
5
+ using System . Data ;
6
+ using System . Collections . Generic ;
7
+
8
+ namespace PandasConsole ;
9
+ public static class Utils
10
+ {
11
+ public static void PrintDataFrameInfo ( DataFrame df )
12
+ {
13
+ int indexLength = df . shape [ 0 ] ;
14
+ float minIndex = df . index . min ( ) ;
15
+ float maxIndex = df . index . max ( ) ;
16
+ int columnCount = df . columns . Count ;
17
+
18
+ StringBuilder sb = new ( ) ;
19
+ sb . AppendLine ( "**Dataframe Info**" ) ;
20
+ sb . AppendLine ( $ "Range Index: { indexLength } entries, { minIndex } to { maxIndex } ") ;
21
+ sb . AppendLine ( $ "Data Columns: (total { columnCount } columns)") ;
22
+ Console . WriteLine ( sb . ToString ( ) ) ;
23
+
24
+
25
+ DataTable table = new DataTable ( ) ;
26
+ table . Columns . Add ( "#" , typeof ( int ) ) ;
27
+ table . Columns . Add ( "Column" , typeof ( string ) ) ;
28
+ table . Columns . Add ( "Non-Null Count" , typeof ( int ) ) ;
29
+ table . Columns . Add ( "DType" , typeof ( string ) ) ;
30
+
31
+ for ( int i = 0 ; i < df . columns . Count ; i ++ )
32
+ {
33
+ table . Rows . Add ( i , df . columns [ i ] . Name , df [ df . columns [ i ] . Name ] . count ( ) , df [ df . columns [ i ] . Name ] . dtype ?? typeof ( String ) ) ;
34
+ }
35
+ Console . Write ( RenderDataTable ( table ) ) ;
36
+ }
37
+
38
+ /// <summary>
39
+ /// Renders a DataTable to a markdown table
40
+ /// </summary>
41
+ /// <remarks>Code from StackOverflow contributor david-liebeherr</remarks>
42
+ /// <param name="table"></param>
43
+ /// <returns></returns>
44
+ public static string RenderDataTable ( DataTable table )
45
+ {
46
+ String GetCellValueAsString ( DataRow row , DataColumn column )
47
+ {
48
+ var cellValue = row [ column ] ;
49
+ var cellValueAsString = cellValue is null or DBNull ? "{{null}}" : cellValue . ToString ( ) ;
50
+
51
+ return cellValueAsString ;
52
+ }
53
+
54
+ var columnWidths = new Dictionary < DataColumn , Int32 > ( ) ;
55
+
56
+ foreach ( DataColumn column in table . Columns )
57
+ {
58
+ columnWidths . Add ( column , column . ColumnName . Length ) ;
59
+ }
60
+
61
+ foreach ( DataRow row in table . Rows )
62
+ {
63
+ foreach ( DataColumn column in table . Columns )
64
+ {
65
+ columnWidths [ column ] = Math . Max ( columnWidths [ column ] , GetCellValueAsString ( row , column ) . Length ) ;
66
+ }
67
+ }
68
+
69
+ var resultBuilder = new StringBuilder ( ) ;
70
+
71
+ resultBuilder . Append ( "| " ) ;
72
+
73
+ foreach ( DataColumn column in table . Columns )
74
+ {
75
+ resultBuilder . Append ( column . ColumnName . PadRight ( columnWidths [ column ] ) ) ;
76
+ resultBuilder . Append ( " | " ) ;
77
+ }
78
+
79
+ resultBuilder . AppendLine ( ) ;
80
+ resultBuilder . Append ( "| " ) ;
81
+ foreach ( DataColumn column in table . Columns )
82
+ {
83
+ resultBuilder . Append ( "-" . PadRight ( columnWidths [ column ] , '-' ) ) ;
84
+ resultBuilder . Append ( " | " ) ;
85
+ }
86
+ resultBuilder . AppendLine ( ) ;
87
+
88
+ foreach ( DataRow row in table . Rows )
89
+ {
90
+ resultBuilder . Append ( "| " ) ;
91
+
92
+ foreach ( DataColumn column in table . Columns )
93
+ {
94
+ resultBuilder . Append ( GetCellValueAsString ( row , column ) . PadRight ( columnWidths [ column ] ) ) ;
95
+ resultBuilder . Append ( " | " ) ;
96
+ }
97
+
98
+ resultBuilder . AppendLine ( ) ;
99
+ }
100
+
101
+ return resultBuilder . ToString ( ) ;
102
+ }
103
+ }
0 commit comments