# Print DataFrame the same way pandas print

**URL:** https://discourse.julialang.org/t/print-dataframe-the-same-way-pandas-print/83311
**Category:** New to Julia
**Tags:** question, dataframes, prettytables
**Created:** [June 24, 2022, 2:03pm UTC](https://discourse.julialang.org/t/print-dataframe-the-same-way-pandas-print/83311 "2022-06-24T14:03:30Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)
#### Post date: [June 24, 2022, 2:03pm UTC](https://discourse.julialang.org/t/print-dataframe-the-same-way-pandas-print/83311/1 "2022-06-24T14:03:30Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [June 24, 2022, 2:20pm UTC](https://discourse.julialang.org/t/print-dataframe-the-same-way-pandas-print/83311/2 "2022-06-24T14:20:47Z")

</div>

```julia
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)

---

<div class="post-metadata">

### Author: ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)
#### Post date: [June 24, 2022, 2:32pm UTC](https://discourse.julialang.org/t/print-dataframe-the-same-way-pandas-print/83311/3 "2022-06-24T14:32:46Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)
#### Post date: [June 24, 2022, 2:37pm UTC](https://discourse.julialang.org/t/print-dataframe-the-same-way-pandas-print/83311/4 "2022-06-24T14:37:34Z")

</div>

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 🙂

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

---

<div class="post-metadata">

### Author: ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)
#### Post date: [June 24, 2022, 2:47pm UTC](https://discourse.julialang.org/t/print-dataframe-the-same-way-pandas-print/83311/5 "2022-06-24T14:47:38Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)
#### Post date: [June 24, 2022, 3:57pm UTC](https://discourse.julialang.org/t/print-dataframe-the-same-way-pandas-print/83311/6 "2022-06-24T15:57:49Z")

</div>

> [@gitboy16](#):
>
> 1/ can you give some code on how to print first/last rows? (If different than what was suggested above)

Currently, what @nilshg is the only way.

> [@gitboy16](#):
>
> 2/ for the columns printing, is there an issue open where this can be tracked?

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.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [June 25, 2022, 8:35am UTC](https://discourse.julialang.org/t/print-dataframe-the-same-way-pandas-print/83311/8 "2022-06-25T08:35:54Z")

</div>

One way using PrettyTables:

```julia
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)

```

 ![PrettyTables_pretty_df_first_last_columns](https://global.discourse-cdn.com/julialang/original/3X/c/e/ce8f099164737384be9baae981273b74486c1792.png)

---

<div class="post-metadata">

### Author: ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)
#### Post date: [June 25, 2022, 12:10pm UTC](https://discourse.julialang.org/t/print-dataframe-the-same-way-pandas-print/83311/9 "2022-06-25T12:10:16Z")

</div>

Thank you i will check it when i can.
