# Delete rows in DataFrame Conditionally

**URL:** <https://discourse.julialang.org/t/delete-rows-in-dataframe-conditionally/34774>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [February 17, 2020, 8:43pm UTC](https://discourse.julialang.org/t/delete-rows-in-dataframe-conditionally/34774 "2020-02-17T20:43:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aedunn12](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@aedunn12](https://discourse.julialang.org/u/aedunn12)\
**Post date:** [February 17, 2020, 8:43pm UTC](https://discourse.julialang.org/t/delete-rows-in-dataframe-conditionally/34774/1 "2020-02-17T20:43:00Z")

</div>

I’m trying to delete specific rows in my DataFrame based on conditionals of the row above. In my data, there is a repeated row of the same time point at each time that a medication is administered. My goal is to delete all repeated rows.

I’ve tried the following and was looking if Queryverse had some type of deletion tool with no luck:

for i = 1:length(infusion\_single.time)  
if infusion\_single.time[i] == infusion\_single.time[i+1]  
delete!(infusion\_single.time[i+1])  
end  
end

Thanks!

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 17, 2020, 8:55pm UTC](https://discourse.julialang.org/t/delete-rows-in-dataframe-conditionally/34774/2 "2020-02-17T20:55:32Z")

</div>

Have you looked at the `unique!` function?

---

<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:** [February 17, 2020, 9:05pm UTC](https://discourse.julialang.org/t/delete-rows-in-dataframe-conditionally/34774/3 "2020-02-17T21:05:07Z")

</div>

If all the duplicates are the exact same row, `unique(df)` will work.

If you are just looking for duplicates where the rows are next to each other, here is a function that will work.

One thing that makes your loop tough is that you are modifying the thing you are looping over, which makes behavior tough to reason about. My function makes a new data frame and then pushes rows to it.

```julia
function dropdupecols(df, cols)
    new_df = DataFrame()
    last_row = df[2,:] # just so we can declare it and update it. Can't use the first row. 
    for row in eachrow(df)
        if row[cols] != last_row[cols]
           push!(new_df, row)
        end
        last_row = row
    end
    return new_df
end

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 17, 2020, 9:59pm UTC](https://discourse.julialang.org/t/delete-rows-in-dataframe-conditionally/34774/4 "2020-02-17T21:59:46Z")

</div>

`unique!` takes a column argument as well, yea?

```julia
unique!(infusion_single,:time)

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [February 18, 2020, 8:36pm UTC](https://discourse.julialang.org/t/delete-rows-in-dataframe-conditionally/34774/5 "2020-02-18T20:36:37Z")

</div>

This should work if I understand correctly that `time` is a numeric column:

```julia
infusion_single = infusion_single[[true; diff(infusion_single.time) .!= 0], :]

```

The `true` keeps the first row because `diff` returns an array one element shorter than the column.
