Julia dataframe : Deleting row from sub dataframe

Viewed 166

Problem statement : deleting row from sub dataframe

Code:

x=[rand(3) for i in 1:3]
dfx=DataFrame(x,:auto)
dfy=@view dfx[2:3,:]

Q: I want to delete first row from dfy so it will be deleted from dfx too. I do subset of original dfx to make further checking of subsetted rows if they fulfill conditions. At the end I want to decide to keep row in dfx or to delete it. I operate on subset of dfx which is dfy.

1 Answers

You are not allowed to perform row deletion in views. Here is one example showing why it would be problematic:

julia> using DataFrames

julia> df = DataFrame(a=1:3)
dfv = 3×1 DataFrame
 Row │ a
     │ Int64
─────┼───────
   1 │     1
   2 │     2
   3 │     3

julia> dfv = view(df, [1, 1, 1, 1], :)
4×1 SubDataFrame
 Row │ a
     │ Int64
─────┼───────
   1 │     1
   2 │     1
   3 │     1
   4 │     1

and now assume you want to remove rows 2 and 3 from the dfv view, but you cannot remove the row from the parent twice and also after such a deletion what would be the state of dfv?

I do subset of original dfx to make further checking of subsetted rows if they fulfill conditions.

Note that you can use parentindices function to get the indices in the parent of your view, so that you can later remove appropriate rows from the parent.

EDIT

An example:

julia> x=[rand(3) for i in 1:3]
3-element Vector{Vector{Float64}}:
 [0.9362990387940191, 0.872386665989372, 0.9062520245175714]
 [0.31161625031197393, 0.21614040488877717, 0.7277794414244152]
 [0.35548885964798926, 0.4422493896149622, 0.45150837090448315]

julia> dfx=DataFrame(x, :auto)
3×3 DataFrame
 Row │ x1        x2        x3       
     │ Float64   Float64   Float64  
─────┼──────────────────────────────
   1 │ 0.936299  0.311616  0.355489
   2 │ 0.872387  0.21614   0.442249
   3 │ 0.906252  0.727779  0.451508

julia> dfy=@view dfx[2:3, :]
2×3 SubDataFrame
 Row │ x1        x2        x3       
     │ Float64   Float64   Float64  
─────┼──────────────────────────────
   1 │ 0.872387  0.21614   0.442249
   2 │ 0.906252  0.727779  0.451508

julia> row_to_remove = parentindices(dfy)[1][1]
2

julia> delete!(dfx, row_to_remove)
2×3 DataFrame
 Row │ x1        x2        x3       
     │ Float64   Float64   Float64  
─────┼──────────────────────────────
   1 │ 0.936299  0.311616  0.355489
   2 │ 0.906252  0.727779  0.451508
Related