# How would I update a DataFrame with new data and delete old data so I can maintain the N most recent rows per person

**URL:** <https://discourse.julialang.org/t/how-would-i-update-a-dataframe-with-new-data-and-delete-old-data-so-i-can-maintain-the-n-most-recent-rows-per-person/42368>\
**Category:** General Usage\
**Created:** [July 1, 2020, 1:49pm UTC](https://discourse.julialang.org/t/how-would-i-update-a-dataframe-with-new-data-and-delete-old-data-so-i-can-maintain-the-n-most-recent-rows-per-person/42368 "2020-07-01T13:49:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julia1](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Julia1](https://discourse.julialang.org/u/Julia1)\
**Post date:** [July 1, 2020, 1:49pm UTC](https://discourse.julialang.org/t/how-would-i-update-a-dataframe-with-new-data-and-delete-old-data-so-i-can-maintain-the-n-most-recent-rows-per-person/42368/1 "2020-07-01T13:49:07Z")

</div>

Hi how’s it going? Here is an example of the dataframe, and a new data row to be added.

```julia
df = DataFrame(:name=>["john","john","john","john","john","mike","mike","mike","mike","mike"]
,:day=>["2020-05-14","2020-05-13","2020-05-12","2020-05-11","2020-05-10","2020-05-14","2020-05-13","2020-05-12","2020-05-11","2020-05-10"],
:earnings=>[20,15,17,32,15,87,65,80,56,90])
df[:day] = DateTime.(df[:day])

new_data = DataFrame(:name=>["john"],:day=>["2020-05-15"],:earnings=>[18])
new_data[:day] = DateTime.(new_data[:day])

```

heres a screenshot for a better view of the data:  
 ![Screenshot from 2020-07-01 09-49-37](https://global.discourse-cdn.com/julialang/original/3X/0/f/0f81396be86a5190b63ec6088fe85e1a358cafb2.png)

![Screenshot from 2020-07-01 09-50-38](https://global.discourse-cdn.com/julialang/original/3X/7/5/75daa317153397e1754a3ad84914d4d5d83a2f29.png)  
In this case, I would like to maintain the 5 most recent data entries per “name”. So the new\_data row should be added to df, and the oldest row for “john” which was on “2020-05-10” should be popped off the back end.

What is the best and most efficient way to implement this?

---

<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:** [July 1, 2020, 2:17pm UTC](https://discourse.julialang.org/t/how-would-i-update-a-dataframe-with-new-data-and-delete-old-data-so-i-can-maintain-the-n-most-recent-rows-per-person/42368/2 "2020-07-01T14:17:10Z")

</div>

You should really start using the `df[:, name]` indexing format, because that will break when DataFrames hits 1.0, which is happening soonish.

I think I would do the following

1. Make the thing you add to the dataframe a named tuple, rather than a one-row data frame
2. use this function

```julia
julia> function add_and_delete!(df, nt::NamedTuple)
       push!(df, nt)
       sort!(df, [:name, :day])
       ind = findfirst(t -> t.name == personname, eachrow(df))
       delete!(df, ind)
       return df
       end

```

---

<div class="post-metadata">

**Author:** ![Julia1](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Julia1](https://discourse.julialang.org/u/Julia1)\
**Post date:** [July 1, 2020, 2:26pm UTC](https://discourse.julialang.org/t/how-would-i-update-a-dataframe-with-new-data-and-delete-old-data-so-i-can-maintain-the-n-most-recent-rows-per-person/42368/3 "2020-07-01T14:26:25Z")

</div>

ok sounds good thank you very much. old habits die hard. but yeah obviously they’ll completely die if the code breaks so I’ll get on that.

Thanks again
