How to delete rows in DataFrame?

group=[:A,:A,:B,:B,:C,:C,:C,:C]
X = 100:108
df = DataFrame(; group, X)
julia> df = DataFrame(; group, X)
8×2 DataFrame
 Row │ group   X
     │ Symbol  Int64
─────┼───────────────
   1 │ A           101
   2 │ A           102
   3 │ B           103
   4 │ B           104
   5 │ C           105
   6 │ C           106
   7 │ C           107
   8 │ C           108

I want to delete the second line that starts with C, so what should I do? I do not want to write thr code like deleteat! (df,6). What I want is that the code should contain C and 2.Thanks for helping me!

julia> deleteat!(df, findall(==(:C), df.group)[2])
7×2 DataFrame
 Row │ group   X     
     │ Symbol  Int64 
─────┼───────────────
   1 │ A         101
   2 │ A         102
   3 │ B         103
   4 │ B         104
   5 │ C         105
   6 │ C         107
   7 │ C         108

Assumes that there is a second line that starts with C i.e. assumes there are at least two Cs in the group column.

2 Likes

Thanks.If I want the code to contain C and 106(delete the sixth row in df),what should I do?I found that the function findall didn’t work.Thanks for helping me!

Could you show what isn’t working? The code that @digital_carver posted above does exactly what you are asking for (delete the row in which X is 106).

If you are now saying you want multiple conditions you can find the index by chaining conditions with .&& like this:

julia> findall(df.group .== :C .&& df.X .== 106)
1-element Vector{Int64}:
 6

Thank you

1 Like