Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: How delete multiple rows by index from a dataframe

I'd like to know how I can permanently delete multiple rows from a dataframe in Julia. Here is the dataframe example:

    Group   Variable1   Variable2
String  Float64 Float64
1   B   -0.661256   0.265538
2   B   0.111651    0.837895
3   A   0.197754    0.987195
4   A   1.35057 0.696815
5   A   -1.20899    0.496407
6   B   0.813047    0.324904

I'd like to delete rows 2, 4, and 6 from my dataframe. There is an easy function to do that?

like image 298
George Carvalho Avatar asked Oct 19 '25 13:10

George Carvalho


2 Answers

If you want to delete inplace:

delete!(df, [2, 4, 6])

In case you want a new df without the selected rows:

df[Not([2, 4, 6]), :]
like image 114
Filipe C L Duarte Avatar answered Oct 21 '25 06:10

Filipe C L Duarte


As of DataFrames.jl version 1.3, delete! has been deprecated and replaced with deleteat!. This change was made to more correctly reflect the difference between Base.delete! and Base.deleteat!. You can compare the docstring for Base.deleteat! to the docstring for Base.delete!. In fact, the DataFrames deleteat! function is a method extension of Base.deleteat!.

Here's an example of using deleteat! on a data frame:

julia> using DataFrames

julia> df = DataFrame(a=1:4, b=5:8)
4×2 DataFrame
 Row │ a      b     
     │ Int64  Int64 
─────┼──────────────
   1 │     1      5
   2 │     2      6
   3 │     3      7
   4 │     4      8

julia> deleteat!(df, [2, 3])
2×2 DataFrame
 Row │ a      b     
     │ Int64  Int64 
─────┼──────────────
   1 │     1      5
   2 │     4      8

Here's the DataFrames documentation for deleteat!:

https://dataframes.juliadata.org/stable/lib/functions/#Base.deleteat!

like image 38
Cameron Bieganek Avatar answered Oct 21 '25 05:10

Cameron Bieganek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!