@@ -2259,6 +2259,52 @@ DataFrame >> toColumnsAt: arrayOfColumnNumbers applyElementwise: aBlock [
22592259 self toColumnAt: each applyElementwise: aBlock ]
22602260]
22612261
2262+ { #category : #converting }
2263+ DataFrame >> toMarkdown [
2264+ " Prints the DataFrame as a Markdown formatted table"
2265+
2266+ | markdown columnWidths dataFrame |
2267+ dataFrame := self copy.
2268+ dataFrame addColumn: dataFrame rowNames named: ' #' atPosition: 1 .
2269+ markdown := WriteStream on: String new .
2270+ markdown nextPutAll: ' | ' .
2271+
2272+ columnWidths := dataFrame columnNames collect: [ :columnName |
2273+ | maxWidth |
2274+ maxWidth := columnName size.
2275+ dataFrame rows do: [ :row |
2276+ | value |
2277+ value := row at: columnName.
2278+ maxWidth := maxWidth max: value printString size ].
2279+ maxWidth ].
2280+
2281+ dataFrame columnNames withIndexDo: [ :columnName :index |
2282+ | paddedColumnName |
2283+ paddedColumnName := columnName padRightTo: (columnWidths at: index).
2284+ markdown nextPutAll: paddedColumnName , ' | ' ].
2285+ markdown cr.
2286+ markdown nextPutAll: ' | ' .
2287+
2288+ columnWidths do: [ :width |
2289+ | secondRow |
2290+ secondRow := ' -' .
2291+ width - 1 timesRepeat: [ secondRow := secondRow , ' -' ].
2292+ markdown nextPutAll: secondRow , ' | ' ].
2293+
2294+ markdown cr.
2295+
2296+ dataFrame asArrayOfRows do: [ :row |
2297+ markdown nextPutAll: ' | ' .
2298+ row withIndexDo: [ :value :index |
2299+ | paddedValue |
2300+ paddedValue := value printString padRightTo:
2301+ (columnWidths at: index).
2302+ markdown nextPutAll: paddedValue , ' | ' ].
2303+ markdown cr ].
2304+
2305+ ^ markdown contents
2306+ ]
2307+
22622308{ #category : #geometry }
22632309DataFrame >> transposed [
22642310 " Returns a transposed DataFrame. Columns become rows and rows become columns."
0 commit comments