# Julia Row-wise operation of DataFrames

**URL:** <https://discourse.julialang.org/t/julia-row-wise-operation-of-dataframes/26315>\
**Category:** New to Julia\
**Tags:** question, dataframes\
**Created:** [July 13, 2019, 8:29am UTC](https://discourse.julialang.org/t/julia-row-wise-operation-of-dataframes/26315 "2019-07-13T08:29:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![POKIN\_CHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pokin_chan/32/9309_2.png) [@POKIN\_CHAN](https://discourse.julialang.org/u/POKIN_CHAN)\
**Post date:** [July 13, 2019, 8:29am UTC](https://discourse.julialang.org/t/julia-row-wise-operation-of-dataframes/26315/1 "2019-07-13T08:29:58Z")

</div>

```
    function substring_(x::String,i::Int32)
        if i==1
            return string(x[1])
        elseif i==2
            return x[2:end]
        else
            return "NA"
        end
    end

```

I would like to apply the function above on a column of dataframe:  
i.e. substring\_.(train[:col],2).  
But it returns an array of single character e.g. [‘a’,‘b’,‘c’…] instead of [“apo”,“bksk”,“cssa”…].

In python, there is an apply + lambda method to do what I want. May I know how Julia can implement this? Thanks.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [July 13, 2019, 9:16am UTC](https://discourse.julialang.org/t/julia-row-wise-operation-of-dataframes/26315/2 "2019-07-13T09:16:54Z")

</div>

Make sure you are actually applying the function you have pasted (you might have changed the definition of `substring_` and not reloaded it), as:

- the method you describe does not return `Char` values;
- the method will not get called by `substring_.(train[:col],2)`, unless you use 32-bit Julia;

Now regarding your function I would recommend to rewrite it as:

```julia
function substring_(x::String,i::Int) # changed Int32 to Int so that we use the default integer type on your machine
        if i==1
            return first(x, 1) # this gives you a string containing the first character of x
        elseif i==2
            return chop(x, head=1, tail=0) # this is a safe way to remove the first character from x; your code would fail on non-ASCII characters
        else
            return "NA"
        end
    end

```

Then:

```julia
substring_.(train.col,2) # note that in DataFrames.jl you can access columns by name as a property

```

will work.

Also in Julia a standard way to indicate a missing value is `missing`, so you might consider using this value instead of `"NA"` in your code (“NA” might be OK for your purposes, so I did not change this in the code. However, note that if you write `substring_("aNA", 2)` you also get `"NA"` returned and then you are not able to distinguish these cases).

If some additional explanations of the above would be helpful please let me know.

---

<div class="post-metadata">

**Author:** ![POKIN\_CHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pokin_chan/32/9309_2.png) [@POKIN\_CHAN](https://discourse.julialang.org/u/POKIN_CHAN)\
**Post date:** [July 13, 2019, 9:36am UTC](https://discourse.julialang.org/t/julia-row-wise-operation-of-dataframes/26315/3 "2019-07-13T09:36:37Z")

</div>

Thanks for your useful advice. I think my function was not reloaded well. Your suggestions help improve my Julia programming skills.
