# Cropping dataframe table data left or center

**URL:** <https://discourse.julialang.org/t/cropping-dataframe-table-data-left-or-center/110344>\
**Category:** General Usage\
**Tags:** question, dataframes, prettytables\
**Created:** [February 17, 2024, 1:57pm UTC](https://discourse.julialang.org/t/cropping-dataframe-table-data-left-or-center/110344 "2024-02-17T13:57:30Z")\
**Posts on this page:** 1\
**Showing post:** 7

<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:** [February 18, 2024, 10:28am UTC](https://discourse.julialang.org/t/cropping-dataframe-table-data-left-or-center/110344/7 "2024-02-18T10:28:19Z")

</div>

See here below a modification of the formatting function following your template.  
I added your ASCII example and also my previous unicode example with a mixed types dataframe.

> **revised code**
>
> ```julia
> using DataFrames
> 
> # function by @StefanKarpinski (https://discourse.julialang.org/t/truncate-string/27978/14)
> slice(s, n, m) = s[max(1, nextind(s, 0, n)):min(end, nextind(s, 0, m))]
> 
> function lstr(s)
> ls = length(s)
> ls ≤ colwidth && return s
> return slice(s, 1, colwidth) * "…"
> end
> 
> function rstr(s)
> ls = length(s)
> ls ≤ colwidth && return s
> return "…" * slice(s, max(1, ls - colwidth + 1), ls)
> end
> 
> function cstr(s)
> ls = length(s)
> ls ≤ colwidth && return s
> r1 = slice(s, 1, colwidth÷2)
> r2 = colwidth÷2 ≥ ls ? "" : slice(s, max(1, ls - colwidth÷2 + 1), ls)
> return r1 * "…" * r2
> end
> 
> # Example#1
> df = DataFrame(strings = ["0123456789", join('a':'z'), "short"])
> colwidth = 10
> colswidths = [colwidth + 3]
> textcols = names(df, String)
> show(transform(df, textcols .=> ByRow(lstr); renamecols=false), alignment=:l, columns_width=colswidths)
> show(transform(df, textcols .=> ByRow(rstr); renamecols=false), alignment=:l, columns_width=colswidths)
> show(transform(df, textcols .=> ByRow(cstr); renamecols=false), alignment=:l, columns_width=colswidths)
> 
> # Example#2
> df = DataFrame(strings = [join('α':'ω'), join('a':'z')])
> df.length = length.(df.strings)
> colwidth = 11 # try also: 30
> colswidths = [colwidth + 3, 7]
> textcols = names(df, String)
> show(transform(df, textcols .=> ByRow(lstr); renamecols=false), alignment=:l, columns_width=colswidths)
> show(transform(df, textcols .=> ByRow(rstr); renamecols=false), alignment=:r, columns_width=colswidths)
> show(transform(df, textcols .=> ByRow(cstr); renamecols=false), alignment=:c, columns_width=colswidths)
> 
> ```

---

_[View the full topic](https://discourse.julialang.org/t/cropping-dataframe-table-data-left-or-center/110344)._
