# Translation groupby and agg and join python to julia

**URL:** https://discourse.julialang.org/t/translation-groupby-and-agg-and-join-python-to-julia/58835
**Category:** General Usage
**Created:** [April 8, 2021, 2:02pm UTC](https://discourse.julialang.org/t/translation-groupby-and-agg-and-join-python-to-julia/58835 "2021-04-08T14:02:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Rafael\_Brus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael_brus/32/23767_2.png) [@Rafael\_Brus](https://discourse.julialang.org/u/Rafael_Brus)
#### Post date: [April 8, 2021, 2:02pm UTC](https://discourse.julialang.org/t/translation-groupby-and-agg-and-join-python-to-julia/58835/1 "2021-04-08T14:02:25Z")

</div>

How do I translate this python code to julia:

V1 = [“co\_seq\_atend”, “co\_seq\_atend\_prof”, “co\_prontuario”, “no\_status\_atend”, “st\_atend\_prof”, “no\_tipo\_atend\_prof”,  
“no\_profissional”, “co\_cbo\_2002”, “nu\_cnes”, “nu\_ine”, “nu\_cpf\_pf”, “nu\_cns\_pf”,  
“nu\_cns”, “no\_cidadao”, “no\_sexo”, “co\_cds\_turno”]

df1.groupby(V1).agg(lambda x: ‘!’.join(set(x))).reset\_index()

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [April 8, 2021, 2:11pm UTC](https://discourse.julialang.org/t/translation-groupby-and-agg-and-join-python-to-julia/58835/2 "2021-04-08T14:11:31Z")

</div>

I don’t know exactly what your code does. But here is a simple split apply combine example that somewhat mirrors your python code

```julia
julia> using DataFramesMeta, Chain, Statistics

julia> df = DataFrame(a = [1, 1, 2, 2], b = [1, 2, 3, 4]);

julia> @chain df begin 
           groupby(:a)
           @combine(b = mean(:b))
       end
2×2 DataFrame
 Row │ a b       
     │ Int64 Float64 
─────┼────────────────
   1 │ 1 1.5
   2 │ 2 3.5

```

---

<div class="post-metadata">

### Author: ![Rafael\_Brus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael_brus/32/23767_2.png) [@Rafael\_Brus](https://discourse.julialang.org/u/Rafael_Brus)
#### Post date: [April 8, 2021, 2:40pm UTC](https://discourse.julialang.org/t/translation-groupby-and-agg-and-join-python-to-julia/58835/3 "2021-04-08T14:40:34Z")

</div>

I want to merge several strings in a dataframe based on a groupedby

![Capturar](https://global.discourse-cdn.com/julialang/original/3X/6/b/6bface965cdbe2113071fdbec5a287f31375bbd3.png)

The python solution I found here:  
[Python solution](https://stackoverflow.com/questions/27298178/concatenate-strings-from-several-rows-using-pandas-groupby)

Thanks

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [April 8, 2021, 2:54pm UTC](https://discourse.julialang.org/t/translation-groupby-and-agg-and-join-python-to-julia/58835/4 "2021-04-08T14:54:27Z")

</div>

Welcome to our community 🎊

I would recommend reading this [PSA](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) for how to ask questions in this forum. It would be nice if you put triple backticks on your code and posted one or two CSVs instead of an image.

```julia
Nate;Text;Month
name1;hej;11
name1;du;11
name1;aj;12
name1;oj;12
name2;finn;11
name2;katt;11
name2;mycket;12
name2;lite;12

```

I believe that what you want is:

```julia
julia> using DataFrames
julia> using CSV
julia> df = DataFrame(CSV.File("data.csv"; delim = ';'))
8×3 DataFrame
 Row │ Nate Text Month 
     │ String String Int64 
─────┼───────────────────────
   1 │ name1 hej 11
   2 │ name1 du 11
   3 │ name1 aj 12
   4 │ name1 oj 12
   5 │ name2 finn 11
   6 │ name2 katt 11
   7 │ name2 mycket 12
   8 │ name2 lite 12

julia> gdf = groupby(df, [:Nate, :Month]) 
GroupedDataFrame with 4 groups based on keys: Nate, Month
First Group (2 rows): Nate = "name1", Month = 11
 Row │ Nate Text Month 
     │ String String Int64 
─────┼───────────────────────
   1 │ name1 hej 11
   2 │ name1 du 11
⋮
Last Group (2 rows): Nate = "name2", Month = 12
 Row │ Nate Text Month 
     │ String String Int64 
─────┼───────────────────────
   1 │ name2 mycket 12
   2 │ name2 lite 12

julia> cdf = combine(gdf, :Text => (xs -> join(xs, '!')))
4×3 DataFrame
 Row │ Nate Month Text_function 
     │ String Int64 String        
─────┼──────────────────────────────
   1 │ name1 11 hej!du
   2 │ name1 12 aj!oj
   3 │ name2 11 finn!katt
   4 │ name2 12 mycket!lite

```

You can already give the name of the new column if you add a `=> :NewColName` after the anonymous function.

---

<div class="post-metadata">

### Author: ![Rafael\_Brus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael_brus/32/23767_2.png) [@Rafael\_Brus](https://discourse.julialang.org/u/Rafael_Brus)
#### Post date: [April 8, 2021, 3:33pm UTC](https://discourse.julialang.org/t/translation-groupby-and-agg-and-join-python-to-julia/58835/5 "2021-04-08T15:33:49Z")

</div>

Work like a charm!

Thanks!!!
