Print DataFrame the same way pandas print

Hi,
Is there a way to print a Julia DataFrame the same way pandas dataframe prints? ( i.e. the 5 first and last rows and first/last columns)
Thank you
Regards.

df[[1:5; end-4:end], :]

(I think there’s also some work ongoing in PrettyTables which might make this sort of start/end printing the default, @Ronis_BR might know)

Thanks, the issue with this answer is that the number of columns to print is fixed and does adapt to the size of the REPL. It won’t always print the last columns.

Hi @gitboy16 !

We have already implemented in text back-end a solution to print the first and last rows. However, printing the first and last columns is very tricky. I did not have yet enough time to add this feature to PrettyTables because there are more important things to do. It is in the planning :slight_smile:

(EDIT: I mean, it is tricky in text backend, in LaTeX and HTML is very easy)

2 Likes

Thank you @Ronis_BR
1/ can you give some code on how to print first/last rows? (If different than what was suggested above)
2/ for the columns printing, is there an issue open where this can be tracked?
Thanks again

Currently, what @nilshg is the only way.

I think not! The problem of the column printing is that I would need to process one column at the beginning and one column at the end repeatedly to check how many columns I can fit on the screen. It does not seem a big deal, but it requires a lot of changes in the internals.

The rows are more easy because I have the number of rows in the screen and then I just process half of it from the beginning and half from the end.

One way using PrettyTables:

using DataFrames, PrettyTables

function dfrc(df, r, c)
    n, m = size(df)
    ix = (n < 2*r) ? (1:n) : [1:r; (n-r+1):n]
    iy = (m < 2*c) ? (1:m) : [1:c; (m-c+1):m]
    dg = copy(df[ix, iy])
    insertcols!(dg, 1, "nrow" => ix)
    (m > 2*c) && insertcols!(dg, 2 + c, "..." => "...")
    (n > 2*r) && (dg = [dg[1:r,:]; DataFrame(fill("...",1,ncol(dg)), names(dg)); dg[(r+1):(2*r),:]])
    return dg
end

df = DataFrame(rand(30,20),:auto)
pretty_table(dfrc(df,5,3), nosubheader=true)

4 Likes

Thank you i will check it when i can.