# Use only the x first letters of strings inside a columns of a DataFrame

**URL:** <https://discourse.julialang.org/t/use-only-the-x-first-letters-of-strings-inside-a-columns-of-a-dataframe/6979>\
**Category:** General Usage\
**Created:** [November 9, 2017, 12:10pm UTC](https://discourse.julialang.org/t/use-only-the-x-first-letters-of-strings-inside-a-columns-of-a-dataframe/6979 "2017-11-09T12:10:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [November 9, 2017, 12:10pm UTC](https://discourse.julialang.org/t/use-only-the-x-first-letters-of-strings-inside-a-columns-of-a-dataframe/6979/1 "2017-11-09T12:10:18Z")

</div>

Hi everyone,

I have a DataFrame with a column that contains strings. I would like to take all those strings, take only the nine first letters and save the resulting column of strings back into the DataFrame.

What is the best way to go about it?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [November 9, 2017, 12:31pm UTC](https://discourse.julialang.org/t/use-only-the-x-first-letters-of-strings-inside-a-columns-of-a-dataframe/6979/2 "2017-11-09T12:31:27Z")

</div>

`df[:column_name] = [s[1:min(length(s), 9)] for s in df[:column_name]]`

---

<div class="post-metadata">

**Author:** ![kdyrhage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdyrhage/32/2326_2.png) [@kdyrhage](https://discourse.julialang.org/u/kdyrhage)\
**Post date:** [November 9, 2017, 2:00pm UTC](https://discourse.julialang.org/t/use-only-the-x-first-letters-of-strings-inside-a-columns-of-a-dataframe/6979/3 "2017-11-09T14:00:28Z")

</div>

You could try something along the lines of:  
`d = DataFrame(strings = ["asd", "jkl", "qwe", "iop", "bnm"])`  
`d[:truncated] = map(s -> s[1:2], d[:strings])`

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 9, 2017, 4:47pm UTC](https://discourse.julialang.org/t/use-only-the-x-first-letters-of-strings-inside-a-columns-of-a-dataframe/6979/4 "2017-11-09T16:47:32Z")

</div>

> [@cstjean](#):
>
> `df[:column_name] = [s[1:min(length(s), 9)] for s in df[:column_name]]`

This could fail for non-ASCII strings. In 0.7, you can use `first(s, 9)`, and more generally you could grab the code from [first and last with nchar by bkamins · Pull Request #23960 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/23960) and do `s[1:min(endof(s), nextind(s, 8))]`

---

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [November 16, 2017, 6:55pm UTC](https://discourse.julialang.org/t/use-only-the-x-first-letters-of-strings-inside-a-columns-of-a-dataframe/6979/5 "2017-11-16T18:55:51Z")

</div>

Since my next question is of the same spirit, I’ll post it here.

Let’s say I have a string of which I want to cut off x letters from the right. What comes before may vary in length, but the part on the right is always of the same length. How would I go about it?
