# How to add rows in a DataFrame

**URL:** <https://discourse.julialang.org/t/how-to-add-rows-in-a-dataframe/51221>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [December 3, 2020, 8:41pm UTC](https://discourse.julialang.org/t/how-to-add-rows-in-a-dataframe/51221 "2020-12-03T20:41:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smith/32/13567_2.png) [@Smith](https://discourse.julialang.org/u/Smith)\
**Post date:** [December 3, 2020, 8:41pm UTC](https://discourse.julialang.org/t/how-to-add-rows-in-a-dataframe/51221/1 "2020-12-03T20:41:11Z")

</div>

In a dataframe, how do we add rows containing numeric data where a column containing the `NAME` is same?

```julia
df1 = DataFrame(NAME = ["PP","PC","PP","AB","AF","AF"], VALUE =[100,200,300,400,500,100],
INCOME = [23,56,100,90,900,100])

```

From `df1`, I want to get to `df2` with rows of `VALUE` and `INCOME` added together when `NAME` is same.

```julia
df2= DataFrame(NAME = ["PP","PC","AB","AF"], VALUE =[400,200,400,600],
INCOME = [123,56,90,1000])

```

Then I want to create another dataframe `df3` that has same number of rows as `df1` but updated numerical data from `df2`. A bit hacky solution for this will be

```julia
df3= rightjoin(df2,df1,on =:NAME,makeunique=true)
df3= select!(df3,Not(:VALUE_1))
df3= select!(df3,Not(:INCOME_1))

```

---

<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:** [December 3, 2020, 8:46pm UTC](https://discourse.julialang.org/t/how-to-add-rows-in-a-dataframe/51221/2 "2020-12-03T20:46:24Z")

</div>

You want `combine` i.e.

```julia
combine(groupby(df1, :NAME), :VALUE => sum, :INCOME => sum)

```

Your plan for updating the values with a `rightjoin` is fine and what I would do.

But you can do all this in one step via `transform`.

```julia
transform(groupby(df1, :NAME), :VALUE => sum, :INCOME => sum)

```

---

<div class="post-metadata">

**Author:** ![Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smith/32/13567_2.png) [@Smith](https://discourse.julialang.org/u/Smith)\
**Post date:** [December 3, 2020, 8:49pm UTC](https://discourse.julialang.org/t/how-to-add-rows-in-a-dataframe/51221/3 "2020-12-03T20:49:49Z")

</div>

Thanks

---

<div class="post-metadata">

**Author:** ![Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smith/32/13567_2.png) [@Smith](https://discourse.julialang.org/u/Smith)\
**Post date:** [December 3, 2020, 9:12pm UTC](https://discourse.julialang.org/t/how-to-add-rows-in-a-dataframe/51221/4 "2020-12-03T21:12:17Z")

</div>

> [@pdeffebach](#):
>
> `combine(groupby(df1, :NAME), :VALUE => sum, :INCOME => sum)`

This requires specifying names of each column I want to add, so could be difficult to use for a dataframe with many columns. Do `Not` and `All` function work with `combine` to select a range of column for `sum `? I did try using them but I get MethodError

---

<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:** [December 3, 2020, 9:19pm UTC](https://discourse.julialang.org/t/how-to-add-rows-in-a-dataframe/51221/5 "2020-12-03T21:19:51Z")

</div>

yeah, broadcasting doesn’t yet work for those. you want `names(df2, Not(...)) .=> sum`

---

<div class="post-metadata">

**Author:** ![Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smith/32/13567_2.png) [@Smith](https://discourse.julialang.org/u/Smith)\
**Post date:** [December 3, 2020, 11:25pm UTC](https://discourse.julialang.org/t/how-to-add-rows-in-a-dataframe/51221/6 "2020-12-03T23:25:07Z")

</div>

Thanks
