# Manipulation of dataframe rows upon repeated values in a given column

**URL:** <https://discourse.julialang.org/t/manipulation-of-dataframe-rows-upon-repeated-values-in-a-given-column/59367>\
**Category:** New to Julia\
**Created:** [April 15, 2021, 6:49pm UTC](https://discourse.julialang.org/t/manipulation-of-dataframe-rows-upon-repeated-values-in-a-given-column/59367 "2021-04-15T18:49:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [April 15, 2021, 6:49pm UTC](https://discourse.julialang.org/t/manipulation-of-dataframe-rows-upon-repeated-values-in-a-given-column/59367/1 "2021-04-15T18:49:56Z")

</div>

I have the following dataframe (in fact, part of a much larger dataframe, where the Key’s repeat arbitrarily, sometimes even more than twice):

```julia
df_ini = DataFrame(
Key = [170, 447, 447, 699, 963, 963, 963, 756], 
Type = ["No", "No", "No", "Yes", "Yes", "Yes", "Yes", "No"],
Situation = ["Closed", "Pending", "Surpassed", "Surpassed", "Pending", "Surpassed", "Faulty", "Surpassed"]
)

```

I would like to manipulate `df_ini` so as to obtain the transformed dataframe:

```julia
df_fin = DataFrame(
Key = [170, 447, 699, 963, 756],
Type = ["No", "No", "Yes", "Yes", "No"],
Situation = ["Closed", "Pending, Surpassed", "Surpassed", "Pending, Surpassed, Faulty", "Surpassed"]
)

```

That is, the rows where the column `Key` are equal and have the corresponding column `Situation` different must be rendered into a single row, such that all the corresponding columns are the same, except for the column `Situation`, which must have a join of the String values in the original cells of the “unmerged” rows (separated by commas).

Thanks in advance.

---

<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:** [April 15, 2021, 7:23pm UTC](https://discourse.julialang.org/t/manipulation-of-dataframe-rows-upon-repeated-values-in-a-given-column/59367/2 "2021-04-15T19:23:11Z")

</div>

I think you want

```julia
julia> combine(groupby(df_ini, :Key), :Type => first, :Situation => (x -> join(x, ", ")))
5×3 DataFrame
 Row │ Key Type_first Situation_function         
     │ Int64 String String                     
─────┼───────────────────────────────────────────────
   1 │ 170 No Closed
   2 │ 447 No Pending, Surpassed
   3 │ 699 Yes Surpassed
   4 │ 963 Yes Pending, Surpassed, Faulty
   5 │ 756 No Surpassed

```

If want to keep the column names you can set them like `:Type => first => :Type`.

As an aside, `Type` is a defined variable in every Julia session

```julia
julia> Float64 isa Type
true

```

so I wouldn’t recommend using it as a variable name.

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [April 15, 2021, 7:29pm UTC](https://discourse.julialang.org/t/manipulation-of-dataframe-rows-upon-repeated-values-in-a-given-column/59367/3 "2021-04-15T19:29:41Z")

</div>

@nilshg Thank you for your prompt reply and solution, which, of course, worked for me as well.

Could you perhaps, however briefly, explain the logic of the command: first the groupby, then the combine operations? I will sure read about them at any rate.

---

<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:** [April 15, 2021, 7:37pm UTC](https://discourse.julialang.org/t/manipulation-of-dataframe-rows-upon-repeated-values-in-a-given-column/59367/4 "2021-04-15T19:37:17Z")

</div>

Glad it helped!

`combine` (and its friend `transform`) together with `groupby` are probably two of the most useful functionalities of the DataFrames package. When you `groupby` a DataFrame, you can think of this as segmenting the DataFrame into separate Sub-DataFrames, the columns of which you can then apply functions to using `combine` or `transform`.

So in the example above, `:Type => first` means “go through each group in my DataFrame, take the `Type` column for that group, and apply the `first` function (which just returns the first value)”.

Similarly, for `:Situation`, we want to take all the values within a group and join them together - for this we need the `join` function, but as that takes two arguments, we apply it as an anonymous function `(x -> join(x, ", ")`, where `x` is the vector of values in the group.

This only scratches the surface, I highly recommend you read the full explanation by one of the main contributors to DataFrames here:

> **[DataFrames.jl minilanguage explained](https://bkamins.github.io/julialang/2020/12/24/minilanguage.html)**
>
> Introduction

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [April 15, 2021, 7:43pm UTC](https://discourse.julialang.org/t/manipulation-of-dataframe-rows-upon-repeated-values-in-a-given-column/59367/5 "2021-04-15T19:43:30Z")

</div>

Thanks again so much @nilshg. I am really excited about my journey into Julia and how friendly the community is as a whole!

Cheers

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [April 15, 2021, 11:23pm UTC](https://discourse.julialang.org/t/manipulation-of-dataframe-rows-upon-repeated-values-in-a-given-column/59367/6 "2021-04-15T23:23:57Z")

</div>

Here’s another link, probably more useful as a reference since it has fewer examples: [Split-apply-combine · DataFrames.jl](https://dataframes.juliadata.org/stable/man/split_apply_combine/)
